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