//这是我重命名按钮的方法,但退出后它不起作用意味着它没有显示用户重命名的原因?
void changeNameOne(){
newNmOne ="";
lidOne=btn.getText().toString();
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Change Name");
alert.setMessage("Type in the text you want to set.");
final EditText ec = new EditText(this);
alert.setView(ec);
ec.setText(lidOne);
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
newNmOne = ec.getText().toString();
btn.setText(newNmOne);
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
btn.setText(lidOne);
}
});
alert.show();
}
答案 0 :(得分:0)
尝试以下更改:
private static final String MYKEY = "myEditText1";
private static final String DEFAULT_TEXT = "I'm a button";
private String newNmOne = DEFAULT_TEXT;
void changeNameOne(){
lidOne=btn.getText().toString();
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Change Name");
alert.setMessage("Type in the text you want to set.");
final EditText ec = new EditText(this);
alert.setView(ec);
ec.setText(lidOne);
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
newNmOne = ec.getText().toString();
btn.setText(newNmOne);
prefs.putString(MYKEY, newNmOne);////////////here this can be done in onpause too
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
btn.setText(lidOne); //not needed
}
});
alert.show();
}
public void onResume(){
newNmOne = prefs.getString(MYKEY, DEFAULT_TEXT);//if not found set default text
btn.setText(newNmOne);
}
根据您的需求进行调整
答案 1 :(得分:-2)
如果您需要在app退出后保留文本,请使用SharedPreferences。请参阅https://developer.android.com/training/basics/data-storage/shared-preferences.html。