Last active
March 19, 2018 10:41
-
-
Save bluemyria/00a5764b2d71ee6e92fa4fe41b9f03a1 to your computer and use it in GitHub Desktop.
Android - 018 - App-Einstellungen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
////////////////////////////////////////////////////////////////////////////////////////////// | |
// in der Main Activity | |
////////////////////////////////////////////////////////////////////////////////////////////// | |
public class MainActivity extends AppCompatActivity | |
{ | |
private Button btnPrefs, btnPrefsAnzeigen; | |
private TextView tv; | |
/** | |
* Die SharedPreferences speichern Benutzereinstellungen und lassen | |
* sich auch problemlos auslesen. Sie werden verwendet fuer | |
* Thema-Auswahl, Schriftfarbe usw. | |
*/ | |
private SharedPreferences sharedPreferences; | |
// =============================================================== | |
private class MyOCL implements View.OnClickListener | |
{ | |
@Override | |
public void onClick(View v) | |
{ | |
if (v == btnPrefs) | |
prefs(); | |
if (v == btnPrefsAnzeigen) | |
prefsAnzeigen(); | |
} | |
} | |
// =============================================================== | |
/** | |
* Es wird die Seite zur Einstellung der Praeferenzen | |
* aufgerufen. | |
*/ | |
private void prefs() | |
{ | |
Intent intent = new Intent(this, PrefsActivity.class); | |
startActivity(intent); | |
} | |
// --------------------------------------------------------------- | |
/** | |
* Auslesen der Einstellungen | |
*/ | |
private void prefsAnzeigen() | |
{ | |
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); | |
tv.setText(String.format("Updates: %s (Wert: %s)\n", | |
sharedPreferences.getBoolean("cbUpdates", false), | |
sharedPreferences.getString("update_intervall", "5000"))); | |
} | |
// --------------------------------------------------------------- | |
/** | |
* Eingestellte Schriftfarbe auf die <code>TextView</code> anwenden. | |
*/ | |
@Override | |
protected void onResume() | |
{ | |
super.onResume(); | |
int farbe = Integer.parseInt(sharedPreferences.getString | |
("schriftfarbe", "0")); | |
/* | |
* #0000FF - blau mit voller Lichtdurchlaessigkeit | |
* #FF0000FF - blau ohne Lichtdurchlaessigkeit | |
* | |
* #FF000000 | #0000FF -> FF0000FF | |
*/ | |
farbe |= 0xFF000000; | |
tv.setTextColor(farbe); | |
} | |
// --------------------------------------------------------------- | |
private void init() | |
{ | |
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); | |
MyOCL ocl = new MyOCL(); | |
btnPrefs = findViewById(R.id.btnPrefs); | |
btnPrefs.setOnClickListener(ocl); | |
btnPrefsAnzeigen = findViewById(R.id.btnPrefsAnzeigen); | |
btnPrefsAnzeigen.setOnClickListener(ocl); | |
tv = findViewById(R.id.tv); | |
} | |
// --------------------------------------------------------------- | |
@Override | |
protected void onCreate(Bundle savedInstanceState) | |
{ | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
// ----------- | |
init(); | |
} | |
// --------------------------------------------------------------- | |
} | |
////////////////////////////////////////////////////////////////////////////////////////////// | |
// in der PrefsActivity | |
////////////////////////////////////////////////////////////////////////////////////////////// | |
public class PrefsActivity extends PreferenceActivity | |
{ | |
@Override | |
public void onCreate(@Nullable Bundle savedInstanceState) | |
{ | |
super.onCreate(savedInstanceState); | |
addPreferencesFromResource(R.xml.my_preferences); | |
} | |
} | |
////////////////////////////////////////////////////////////////////////////////////////////// | |
// in res/xml/my_preferences.xml | |
////////////////////////////////////////////////////////////////////////////////////////////// | |
<?xml version="1.0" encoding="utf-8"?> | |
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> | |
<!-- ========================================================= --> | |
<PreferenceCategory | |
android:title="Kategorie 1"> | |
<CheckBoxPreference | |
android:defaultValue="true" | |
android:key="cbUpdates" | |
android:title="automatische Updates" | |
android:summary="Ein-/Auschalten der automatischen Updates"/> | |
</PreferenceCategory> | |
<!-- ========================================================= --> | |
<PreferenceCategory | |
android:title="Kategorie 2"> | |
<ListPreference | |
android:defaultValue="1000" | |
android:dependency="cbUpdates" | |
android:entries="@array/updateIntervallBeschriftung" | |
android:entryValues="@array/updateIntervallWerte" | |
android:key="update_intervall" | |
android:title="Update-Intervall" | |
android:summary="Wie oft wird geupdatet?"/> | |
</PreferenceCategory> | |
<!-- ========================================================= --> | |
<PreferenceCategory | |
android:title="Kategorie 3"> | |
<ListPreference | |
android:defaultValue="0" | |
android:entries="@array/schriftfarbeBeschr" | |
android:entryValues="@array/schriftfarbeWert" | |
android:key="schriftfarbe" | |
android:title="Schriftfarbe" | |
android:summary="Schriftfarbe für die TextView"/> | |
</PreferenceCategory> | |
<!-- ========================================================= --> | |
</PreferenceScreen> | |
////////////////////////////////////////////////////////////////////////////////////////////// | |
// in res/values/arrays.xml | |
////////////////////////////////////////////////////////////////////////////////////////////// | |
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<string-array name="updateIntervallBeschriftung"> | |
<item name="1000">Jede Sekunde</item> | |
<item name="5000">Alle 5 Sekunden</item> | |
<item name="30000">Alle 30 Sekunden</item> | |
<item name="60000">Jede Minute</item> | |
<item name="300000">Alle 5 Minuten</item> | |
</string-array> | |
<string-array name="updateIntervallWerte"> | |
<item name="1000">1000</item> | |
<item name="5000">5000</item> | |
<item name="30000">30000</item> | |
<item name="60000">60000</item> | |
<item name="300000">300000</item> | |
</string-array> | |
<string-array name="schriftfarbeBeschr"> | |
<item name="schwarz">Schwarz</item> | |
<item name="rot">Rot</item> | |
<item name="gruen">Gruen</item> | |
<item name="blau">Blau</item> | |
</string-array> | |
<string-array name="schriftfarbeWert"> | |
<item name="schwarz">0</item> | |
<item name="rot">16711680</item> | |
<item name="gruen">65280</item> | |
<item name="blau">255</item> | |
</string-array> | |
</resources> | |
////////////////////////////////////////////////////////////////////////////////////////////// | |
// in AndroidManifest.xml | |
////////////////////////////////////////////////////////////////////////////////////////////// | |
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="....w3t3_anwendungseinstellungen_dozent"> | |
<application | |
android:allowBackup="true" | |
android:icon="@mipmap/ic_launcher" | |
android:label="@string/app_name" | |
android:roundIcon="@mipmap/ic_launcher_round" | |
android:supportsRtl="true" | |
android:theme="@style/AppTheme"> | |
<activity android:name=".MainActivity"> | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN"/> | |
<category android:name="android.intent.category.LAUNCHER"/> | |
</intent-filter> | |
</activity> | |
<activity android:name=".PrefsActivity"/> | |
</application> | |
</manifest> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment