我正在尝试建立一个设置菜单,但我遇到了一些麻烦...
EditTextPreference
sharedPreferences
文件我可以帮忙吗?
Prefs Class
@Override
protected void onResume() {
super.onResume();
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
sharedPreferences.registerOnSharedPreferenceChangeListener(this);
}
@Override
protected void onPause() {
super.onPause();
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
sharedPreferences.unregisterOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
SharedPreferences.Editor editor = sharedPreferences.edit();
contact1 = (EditTextPreference) findPreference("contact1");
editor.putString("contact1", String.valueOf(contact1)).commit();
}
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
}
Prefs XML
<?xml version="1.0" encoding="utf-8"?>
<EditTextPreference
android:title="Contact 1"
android:key="@+id/contact1"
android:summary="Enter your contact">
</EditTextPreference>
<EditTextPreference
android:title="Contact 2"
android:key="@+id/contact2"
android:summary="Enter your contact">
</EditTextPreference>
获取有关主要活动的数据
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
number1 = settings.getString("contact1", "");
number2 = settings.getString("contact2", "");
contact1 = "tel: " + number1;
contact2 = "tel: " + number2;
答案 0 :(得分:1)
使用这些方式...
public class AdvancePreferenceExample extends PreferenceActivity implements OnSharedPreferenceChangeListener{
private static final String KEY_EDIT_TEXT_PREFERENCE = "name";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
@Override
protected void onResume(){
super.onResume();
// Set up a listener whenever a key changes
getPreferenceScreen().getSharedPreferences()
.registerOnSharedPreferenceChangeListener(this);
updatePreference(KEY_EDIT_TEXT_PREFERENCE);
}
@Override
protected void onPause() {
super.onPause();
// Unregister the listener whenever a key changes
getPreferenceScreen().getSharedPreferences()
.unregisterOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
updatePreference(key);
}
private void updatePreference(String key){
if (key.equals(KEY_EDIT_TEXT_PREFERENCE)){
Preference preference = findPreference(key);
if (preference instanceof EditTextPreference){
EditTextPreference editTextPreference = (EditTextPreference)preference;
if (editTextPreference.getText().trim().length() > 0){
editTextPreference.setSummary("Entered Name is " + editTextPreference.getText());
}else{
editTextPreference.setSummary("Enter Your Name");
}
}
}
}
}