我试图让偏好对用户输入作出反应,但没有运气。我正在使用PreferenceFragment,似乎我到目前为止找不到的单个代码可以帮助我。
这是我的偏好.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen " >
<PreferenceCategory
"
" >
<CheckBoxPreference
"
"
o aplicativo para uso"
aplicativo" />
</PreferenceCategory>
</PreferenceScreen>
这是我的屏幕代码:
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);
}
}
}
我想要的是,当我触摸首选项时,它会显示一个祝酒词。代码似乎没问题(没有错误),但它不起作用。我想我听不到听众背后的想法。有人可以帮助我吗?
谢谢!
在我忘记之前,我正在使用 API级别19 (Android 4.4)。我不知道这是否有所改变。
答案 0 :(得分:1)
我建议你使用
Preference.OnPreferenceClickListener
您可以在此处找到更多相关信息: http://developer.android.com/reference/android/preference/Preference.OnPreferenceClickListener.html
示例:
Preference p = findPreference("yourkey");
p.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference p) {
// TODO stuff
return true;
}
});
修改有关弃用:
PreferenceActivity.findPreference("key")
- &gt; deprecated
PreferenceFragment.findPreference("key")
- &gt; not deprecated
答案 1 :(得分:1)
缺少听众的注册?这就是我这样做的方式:
// 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();
}