我在 android studio 中编写一个 android 应用程序。我的配置文件视图类扩展了允许用户更改其名称和说明的activity_profile_view.xml活动。但是每次调用 onCreate 或 onResume 方法时,都会显示默认值。不会显示任何应该保存在 onClick 或 onPause 中的值。谢谢,这是我的第一篇文章。
public class ProfileView extends Activity implements View.OnClickListener{
public EditText NameEdit, AboutEdit;
public Button SaveButton;
public ImageButton NewPic;
public static final String Profile_Prefs = "Pro_File";
public static SharedPreferences profile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile_view);
NameEdit = (EditText) findViewById(R.id.NameEdit);
AboutEdit = (EditText) findViewById(R.id.AboutEdit);
SaveButton = (Button) findViewById(R.id.SaveButton);
SaveButton.setOnClickListener(this);
//Restore Preferences
profile = this.getSharedPreferences(Profile_Prefs, 0);
AboutEdit.setText(profile.getString("about", "About Me"));
NameEdit.setText(profile.getString("name","name"));
}
@Override
public void onClick(View v) {
SharedPreferences profile = getSharedPreferences(Profile_Prefs, 0);
SharedPreferences.Editor editor = profile.edit();
editor.putString(NameEdit.toString(),"name");
editor.putString(AboutEdit.toString(),"about");
editor.apply();
Intent mainIntent = new Intent(this, Main.class);
startActivity(mainIntent);
};
@Override
protected void onPause(){
super.onPause();
SharedPreferences profile = getSharedPreferences(Profile_Prefs, 0);
SharedPreferences.Editor editor = profile.edit();
editor.putString(NameEdit.toString(),"name");
editor.putString(AboutEdit.toString(),"about");
editor.commit();
}
@Override()
protected void onResume(){
super.onResume();
profile = this.getSharedPreferences(Profile_Prefs, 0);
AboutEdit.setText(profile.getString("about", "About Me"));
NameEdit.setText(profile.getString("name","name"));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.profile_view, menu);
return true;
}
修复 onClick 和 onPause 后,当调用 onResume 或 onCreate 时,我在应用程序的编辑文本字段中得到休耕。
android.widget.EditText{1a0ea60cVFED..中联...32,263736,426#7f0a0003 app:id/NameEdit} android.widget.EditText{651555eVFED..CL....92456-676,796#7f0a0009 app:id/AboutEdit}
我认为在OnClick()
方法中您需要更改它:
@Override
public void onClick(View v) {
SharedPreferences profile = getSharedPreferences(Profile_Prefs, 0);
SharedPreferences.Editor editor = profile.edit();
editor.putString(NameEdit.toString(),"name");
editor.putString(AboutEdit.toString(),"about");
editor.apply();
Intent mainIntent = new Intent(this, Main.class);
startActivity(mainIntent);
};
有了这个:
@Override
public void onClick(View v) {
SharedPreferences profile = getSharedPreferences(Profile_Prefs, 0);
SharedPreferences.Editor editor = profile.edit();
editor.putString("name",NameEdit.getText().toString()); //HERE I THINK YOU INVERTED KEY,VALUE ORDER, THIS IS THE CORRECT WAY
editor.putString("about",AboutEdit.getText().toString()); //INVERTED KEY,VALUE ORDER HERE TOO
editor.apply();
Intent mainIntent = new Intent(this, Main.class);
startActivity(mainIntent);
};
希望这有帮助
编辑:我没有注意到,但你在onPause()方法中有相同的:
@Override
protected void onPause(){
super.onPause();
SharedPreferences profile = getSharedPreferences(Profile_Prefs, 0);
SharedPreferences.Editor editor = profile.edit();
editor.putString("name",NameEdit.getText().toString()); //I THINK THIS IS THE CORRECT WAY
editor.putString("about",AboutEdit.getText().toString()); //I THINK THIS IS THE CORRECT WAY
editor.commit();
}