Elenco preferenza, come fare qualcosa quando si fa clic sulla preferenza
-
12-12-2019 - |
Domanda
OK Quindi ogni post che trovo che non riesco a far funzionare questo, sto cercando di includere la mia lista di preferenza
ImpostazioniBasic.xml
<ListPreference
android:title="themes"
android:summary="Select the option of the list"
android:key="listPref"
android:entries="@array/Themes"
android:entryValues="@array/list"
android:defaultValue="default" />
.
Ora come puoi vedere sopra questa è la mia lista di riempimento all'interno del mio file ImpostazioniBasic.xml. Ora quello che ho bisogno di sapere come fare è che ho 2 file Java, la mia attività principale. e il mio file Java preferenze. Ho bisogno di sapere come posso quando l'utente fa clic su una delle voci che fa qualcosa, mi piace apre qualcosa o cambia l'interfaccia utente, solo qualcosa che penso di poterlo prendere da lì. Ho solo bisogno di sapere come e dove il codice sarebbe andato. all'interno dell'attività principale di acitivty o preferenze.
Ecco il mio file Java Preference
public class Prefs extends PreferenceActivity {
ListPreference listPref;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settingsbasic);
}
protected void onResume() {
super.onResume();
// Registers a callback to be invoked whenever a user changes a preference.
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
protected void onPause() {
super.onPause();
// Unregisters the listener set in onResume().
// It's best practice to unregister listeners when your app isn't using them to cut down on
// unnecessary system overhead. You do this in onPause().
getPreferenceScreen()
.getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
// Sets refreshDisplay to true so that when the user returns to the main
// activity, the display refreshes to reflect the new settings.
WebViewClientDemoActivity.????? = true;
}
}
.
Qualsiasi codice di esempio aiuterà o aggiungere il mio codice sopra. Ho solo bisogno di qualcuno che può far luce su questo codice. Ho provato così tante cose diverse e nessuno di questi funziona.
OK Quindi usare il metodo dell'app di esempio raccomandato di seguito qui è un altro codice che ho.
Main Activity
public class WebViewClientDemoActivity extends Activity {
public static String sPref = null;
public void onStart() {
super.onStart();
// Gets the user's network preference settings
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
// Retrieves a string value for the preferences. The second parameter
// is the default value to use if a preference value is not found.
sPref = sharedPrefs.getString("listPref", "Default");
}
. Soluzione
Questo è il modo in cui ho usato la preferenza:
public class EditPrefs extends PreferenceActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.screen_prefs);
}
}
.
Quindi la cartella IN / RES / XML Ho il file XML:
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
android:key="txtColor"
android:title="textView Color"
android:summary="select color for textViews"
android:entries="@array/txtColor"
android:entryValues="@array/txtColorValues" />
</PreferenceScreen>
.
E IN / RIS / VALORI I DA Questo file XML include elementi e i loro valori:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="txtColor">
<item>blue</item>
<item>brown</item>
<item>gray</item>
<item>violet</item>
</string-array>
<string-array name="txtColorValues">
<item>#ff000099</item>
<item>#5F1E02</item>
<item>#333333</item>
<item>#421C52</item>
</string-array>
</resources>
.
Allora chiamare facilmente questa classe da un'altra classe Android ad esempio quando l'utente fa clic su una voce di menu:
startActivity(new Intent(this, EditPrefs.class));
.
Puoi chiamare le preferenze in ONCreate e Onresume come:
@Override
protected void onResume() {
super.onResume();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String txtColor = prefs.getString("txtColor", DEFAULT COLOR); // for instanse : #ff000099
textView.setBackgroundColor(Color.parseColor(txtColor));
}
. Altri suggerimenti
Ho risolto la mia domanda con questo.
Prefs
public static String theme = "Theme1";
@Override
public void onBackPressed() {
ListPreference listPreference = (ListPreference) findPreference("listPref");
String currValue = listPreference.getValue();
theme = currValue;
super.onBackPressed();
}
.
Attività principale
if (Prefs.theme.equals("Theme1"))
setContentView(R.layout.main);
else
setContentView(R.layout.main2);
.
Preference xml
<ListPreference
android:title="themes"
android:summary="Select the option of the list"
android:key="listPref"
android:entries="@array/listReturnValue"
android:entryValues="@array/listDisplayWord" />
.