它在我编辑时更新前台活动,或者只是在首选项屏幕中的任何输入上单击“确定”,而不是必须先隐藏前台活动。它给我零按钮和偏好xml我给了右键和defaultValue。
所以..场景:我进入偏好设置屏幕,点击编辑按钮文字,只需点击确定,返回,按钮上的“0”,隐藏前台活动,返回前台活动,theres是正确的值。
我希望我包含所有必要的部分。
private SharedPreferences prefs;
private String mobileNumber;
private static final int MENU_PREFERENCES = Menu.FIRST;
private static final int SHOW_PREFERENCES = 1;
private void updateFromPreferences() {
mobileNumber = prefs.getString("mobileNumber", "0");
}
private void refreshAplication(){
button.setText(mobileNumber);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
updateFromPreferences();
refreshAplication();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case (MENU_PREFERENCES): {
Intent i = new Intent(this, Preferences.class);
startActivityForResult(i, SHOW_PREFERENCES);
return true;
}
}
return false;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SHOW_PREFERENCES)
if (resultCode == Activity.RESULT_OK) {
updateFromPreferences();
refreshAplication();
}
}
public class Preferences extends PreferenceActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.userpreferences);
}
}