我使用共享首選項來存儲動態創建的按鈕,並使用它來重命名後存儲動態生成的按鈕的標籤。應用程序工作正常,直到生成按鈕,但問題是與標籤。如果將三個按鈕標記爲Test1,Test2,Test3等。但重新啓動應用程序後,所有生成的按鈕的標籤都是Test3。 Code in MainActivity
共享首選項重新啓動後返回上一個值?
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();
}
您使用的是相同的字符串「鍵」來保存所有按鈕的標籤被覆蓋。您保存的最後一個標籤保留在共享首選項中。 – Uttam
@Andrew T.你可以建議編輯該 –
使用「鍵」+ n - 爲第n個按鈕作爲存儲值的關鍵 – user2450263