Android 重新启动应用程序后,动态创建的按钮是否具有相同的标签?
我正在使用一个Android 重新启动应用程序后,动态创建的按钮是否具有相同的标签?,android,sharedpreferences,dynamically-generated,Android,Sharedpreferences,Dynamically Generated,我正在使用一个预定义按钮在单击它时生成新按钮。生成新按钮后,我希望更改其标签,因为我正在使用对话框中定义的编辑文本,该对话框将弹出仅长按新生成按钮的。要存储所有生成的按钮及其标签,我将使用共享首选项。但问题是重新启动后,所有生成的按钮上都有相同的标签 code in mainactivity----- SharedPreferences prefs=null; String key; int btncount = 15; code in onCreate method---- prefs =
预定义按钮
在单击它时生成新按钮
。生成新按钮后,我希望更改其标签
,因为我正在使用对话框中定义的编辑文本
,该对话框将弹出仅长按新生成按钮的。要存储所有生成的按钮及其标签,我将使用共享首选项
。但问题是重新启动后,所有生成的按钮上都有相同的标签
code in mainactivity-----
SharedPreferences prefs=null;
String key;
int btncount = 15;
code in onCreate method----
prefs = PreferenceManager.getDefaultSharedPreferences(this);
btncount=prefs.getInt("count", 0);
LinearLayout ll = (LinearLayout)findViewById(R.id.layout1);
for(int i=0;i<btncount;i++)
{
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final Button myButton = new Button(this);
myButton.getId();
myButton.setText(prefs.getString(key+myButton.getId(),"New"));
myButton.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View arg0)
{
AlertDialog lbldialog = new AlertDialog.Builder(context).create();
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.getId(), myButton.getText().toString());
edit.commit();
}
});
lbldialog.show();
return true;
}
});
ll.addView(myButton, lp);}
Code to create button-----
if(v == btnaddnew)
{
final Button btn1 = new Button(this);
btn1.setText("New");
btn1.setId(btncount);
btn1.setOnClickListener(new OnClickListener () {
@Override
public void onClick(View v){
reportDialog(btn1.getText().toString());
}
});
btn1.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View arg0) {
AlertDialog lbldialog = new AlertDialog.Builder(context).create();
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.getId(), btn1.getText().toString());
edit.commit();
}
});
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);
btncount++;
Editor editor = prefs.edit();
editor.putInt("count", btncount);
editor.commit();
}
main活动中的代码-----
SharedReferences prefs=null;
字符串键;
int btncount=15;
onCreate方法中的代码----
prefs=PreferenceManager.getDefaultSharedPreferences(此);
btncount=prefs.getInt(“count”,0);
LinearLayout ll=(LinearLayout)findViewById(R.id.layout1);
对于(int i=0;i我在代码中看到一个问题
您可以编写以下代码:
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final Button myButton = new Button(this);
myButton.setText(prefs.getString(key+myButton.getId(),"New"));
您创建了一个新按钮,因此此按钮没有任何id,因此您希望如何从中检索getId()
?此行检索nullnull
。因为getId()
为null,并且键也为null。您需要更改该代码
您尝试使用Key+btn1.getId()
在SP中设置按钮的键,该键为null
,为什么不使用btn1.getId()
作为SP
中的键
要检索标签,只需在for语句中使用i
。您总是使用new
标签创建按钮,必须首先检查SP
是否为空,然后使用new
创建,否则需要将标签设置为存储在SP
@Shayan pourvatan中的值。首先,我正在使用标签创建新按钮“新建”之后,我将其标签更改为test1、test2、test3……但重新启动后,所有按钮的标签均为test3Duplicate question[和[同样,当您从SP
还原值时,能否向我们显示?@Shayan pourvatan共享首选项文件包含以下条目:labeklabel 34