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时,我会在app中的editText字段中进行操作。
android.widget.EditText {1a0ea60cVFED..CL.F ...... 32,263736,426#7f0a0003 app:id / NameEdit} android.widget.EditText {651555eVFED..CL ........ 92456-676,796#7f0a0009 app:id / AboutEdit}
答案 0 :(得分:0)
我认为在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();
}