I'm trying to make a preference react to a user-input but with no luck. I'm using PreferenceFragment and seems that not a single code I found so far can help me.
Here is my preferences.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen " >
<PreferenceCategory
"
" >
<CheckBoxPreference
"
"
o aplicativo para uso"
aplicativo" />
</PreferenceCategory>
</PreferenceScreen>
And here is my screen code:
package com.gh0st.gerenciador;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class TelaPreferencias extends PreferenceActivity implements
OnSharedPreferenceChangeListener {
public static final String KEY_PREF_SYNC_CONN = "prefs_ativar";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment()).commit();
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.tela_preferencias, menu);
return true;
}
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals(KEY_PREF_SYNC_CONN)) {
Toast.makeText(this, "Teste", Toast.LENGTH_SHORT).show();
}
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class SettingsFragment extends PreferenceFragment {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
}
}
I want that, when I touch on the preference, it shows a toast. The code seems to be fine (no errors), but it doesn't work. I think I didn't get the idea behind the listener. Can somebody help me out?
Thanks!
Before I forget, I'm using API level 19 (Android 4.4). I don't know if this changes something.
I suggest you make use of the
Preference.OnPreferenceClickListener
You can find more about it here: http://developer.android.com/reference/android/preference/Preference.OnPreferenceClickListener.html
Example:
Preference p = findPreference("yourkey");
p.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference p) {
// TODO stuff
return true;
}
});
Edit concerning deprecation :
PreferenceActivity.findPreference("key")
--> deprecated
PreferenceFragment.findPreference("key")
--> not deprecated
Missing the registration of the listener? This is how I do that:
// Register the listener
@Override
protected void onResume()
{
// Register OnSharedPreferenceChangeListener
PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).
registerOnSharedPreferenceChangeListener(this);
// Fire the base method
super.onResume();
}
// Unregister the listener
@Override
protected void onPause()
{
// Unregister OnSharedPreferenceChangeListener
PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).
unregisterOnSharedPreferenceChangeListener(this);
// Fire the base method
super.onPause();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.