Android 共享首选项是否在重新启动后返回最后一个值?
我使用共享首选项来存储动态创建的按钮,还使用它来存储重命名后动态生成的按钮的标签。应用程序在生成按钮之前工作正常,但问题在于标签。如果将三个按钮标记为Test1、Test2、Test3等。但重新启动应用程序后,所有生成的按钮上都有标签Test3。Android 共享首选项是否在重新启动后返回最后一个值?,android,sharedpreferences,dynamically-generated,Android,Sharedpreferences,Dynamically Generated,我使用共享首选项来存储动态创建的按钮,还使用它来存储重命名后动态生成的按钮的标签。应用程序在生成按钮之前工作正常,但问题在于标签。如果将三个按钮标记为Test1、Test2、Test3等。但重新启动应用程序后,所有生成的按钮上都有标签Test3。MainActivity中的code SharedPreferences prefs=null; int count = 0; "Code in onCreate method" prefs = PreferenceManager.getDefau
MainActivity中的code
SharedPreferences prefs=null;
int count = 0;
"Code in onCreate method"
prefs = PreferenceManager.getDefaultSharedPreferences(this);
count=prefs.getInt("count", 0);
LinearLayout ll = (LinearLayout)findViewById(R.id.layout1);
for(int i=0;i<count;i++)
{
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final Button myButton = new Button(this);
myButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
reportDialog(myButton.getText().toString());
}
});
myButton.getId();
myButton.setText(prefs.getString("key","New"));
myButton.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View arg0)
{
AlertDialog lbldialog = new AlertDialog.Builder(context).create();
lbldialog.setTitle("Change Button Label");
lbldialog.setIcon(android.R.drawable.ic_dialog_info);
lbldialog.setMessage("Enter new Button label to change");
final EditText input = new EditText(MainActivity.this);
lbldialog.setView(input);
lbldialog.setButton(DialogInterface.BUTTON_POSITIVE, "Change", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which)
{
myButton.setText(input.getText());
Editor edit = prefs.edit();
edit.putString("key", myButton.getText().toString());
edit.commit();
}
});
lbldialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which)
{
Toast.makeText(getApplicationContext(), "Button Label not Changed",Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
lbldialog.show();
return true;
}
});
ll.addView(myButton, lp);
}
"Code to add new buttons:"
if(v == btnaddnew)
{ final Button btn1 = new Button(this);
btn1.setText("New");
btn1.setId(23);
btn1.setOnClickListener(new OnClickListener () {
@Override
public void onClick(View v){
rptDialog(btn1.getText().toString());
}
})
btn1.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View arg0) {
//Dialog Box pops up with edit text field to change button label
AlertDialog lbldialog = new AlertDialog.Builder(context).create();
lbldialog.setTitle("Change Button Label");
lbldialog.setIcon(android.R.drawable.ic_dialog_info);
lbldialog.setMessage("Enter new Button label to change");
final EditText input = new EditText(MainActivity.this);
lbldialog.setView(input);
lbldialog.setButton(DialogInterface.BUTTON_POSITIVE, "Change",
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
btn1.setText(input.getText());
Editor edit = prefs.edit();
edit.putString("key", btn1.getText().toString());
edit.commit();
}
});
lbldialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "Button Label not Changed",Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
lbldialog.show();
return true;
}
});
LinearLayout ll = (LinearLayout)findViewById(R.id.layout1);
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ll.addView(btn1, lp);
count++;
Editor editor = prefs.edit();
editor.putInt("count", count);
editor.commit();
}
SharedReferences prefs=null;
整数计数=0;
“onCreate方法中的代码”
prefs=PreferenceManager.getDefaultSharedPreferences(此);
count=prefs.getInt(“count”,0);
LinearLayout ll=(LinearLayout)findViewById(R.id.layout1);
对于(int i=0;i您对所有按钮使用相同的键:
btn1.setText(input.getText());
Editor edit = prefs.edit();
edit.putString("key", btn1.getText().toString());
edit.commit();
您应该为每一个键创建不同的键,如key1、key2和key3。作为for循环的内部-(?)
“i”将来自for循环,然后使用相同的字符串“key”保存所有按钮的标签,这样它就会被覆盖。您保存的最后一个标签将保留在SharedReferences中。@Andrew T。您可以建议编辑该“key”吗+n-将第n个按钮作为存储value@user2450263是否需要将“n”声明为整数、布尔值etc@user3172071检查答案。另外,如果您使用“n”,则是一个整数。但也可以使用“i”作为for循环
Editor edit = prefs.edit();
edit.putString("key1", myButton.getText().toString());
edit.commit();
Editor edit = prefs.edit();
edit.putString("key2", btn1.getText().toString());
edit.commit();
Editor edit = prefs.edit();
edit.putString("key1", myButton.getText().toString());
edit.commit();
Editor edit = prefs.edit();
edit.putString("key2", btn1.getText().toString());
edit.commit();