【问题标题】:How to change button label on runtime?如何在运行时更改按钮标签?
【发布时间】:2017-06-01 20:29:30
【问题描述】:

我正在使用一个按钮在运行时创建新按钮,但创建的新按钮具有预定义的标签,即btn1.setText("New");。有没有什么办法可以使在运行时创建的按钮可以修改或更改为其他标签,例如 New 它可以是“单击”、“鼠标”、“服装”,但在应用程序中在跑。 创建按钮的代码:

if(v == add){
        Button btn1 = new Button(this);
        btn1.setText("New");
        btn1.setId(34);     
        btn1.setOnClickListener(this);
        btn1.setOnClickListener(new OnClickListener () {
            @Override
            public void onClick(View v){
                mDialog();
            }
        });
        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();
    }

我正在使用共享首选项来存储以前创建的按钮

prefs = PreferenceManager.getDefaultSharedPreferences(this);
    count=prefs.getInt("count", 0);
    LinearLayout ll = (LinearLayout)findViewById(R.id.layout1);
    for(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){
                mDialog(myButton.getText().toString());
            }
        });
        myButton.setId(34);
        myButton.setText("New");
        myButton.setOnLongClickListener(new OnLongClickListener() {
            public boolean onLongClick(View arg0) {
                AlertDialog mdialog = new AlertDialog.Builder(context).create();
                mdialog.setTitle("Change Button Label");
                mdialog.setIcon(android.R.drawable.ic_dialog_info); 
                mdialog.setMessage("Enter new Button label  to change");
                final EditText input = new EditText(MainActivity.this);

                mdialog.setView(input);
                mdialog.setButton(DialogInterface.BUTTON_POSITIVE, "Change",
                        new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) 
                    {
                        myButton.setText(input.getText());
                    }
                });

                mdialog.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();

                                }
                            });
                mdialog.show(); 

                return true;    // <- set to true
            }
        });
        ll.addView(myButton, lp);
    }

【问题讨论】:

  • 是的,您可以,在stackoverflow.com/questions/21497816/… 上查看我的回答,声明您的按钮并为此使用setText 方法。
  • setText 不起作用?
  • 你能接受用户输入的标签吗?或者这个动态标签文本来自哪里
  • 将所有标签存储在ArrayList中,并根据您的要求获取它们。或增量条件
  • @user2450263 来自用户意味着用户将能够根据他的要求更改按钮标签。

标签: android button


【解决方案1】:

您只需:

mButton.setText("WhatEver");

您无需再次将按钮添加到布局中

要保存文本,如果您想一直显示它,如果应用程序关闭,您必须使用共享首选项,例如:

SharedPreference preference = PreferenceManager.getDefaultSharedPreferece(this);

现在,如果用户更改文本,请执行此操作

Editor edit = preference.edit();
edit.putString("key",mButton.getText().toString());
edit.commit();

现在您已使用键“key”将字符串保存在首选项中

现在删除您将按钮文本设置为“新建”的行:

mButton.setText(preference.getString("key","New");

就是这样:)

【讨论】:

  • 但这需要在代码中定义。但我希望用户在运行应用程序时能够命名按钮
  • 然后在某处添加一个 Edittext,它设置 Button 的文本,如:mButton.setText(mEdittext.getText());
  • 请检查我的编辑。我正在使用提供 EditText 的对话框,但在重新命名按钮后,当我重新打开应用程序时,按钮标签仍设置为“新建”。
  • 是的,因为您每次启动时都将Button的值设置为“New”,如果您想一直保存Button的值是什么,您可以使用SharedPreferences跨度>
  • 如果可能的话,你能告诉我怎么做吗
【解决方案2】:

将标签名称设置为 TextView 的动态方式

String[] buttonName = {"new","click","mouse","clothing"};

mButton.setText(buttonName[1]);  // here you can replace 1 with 0 or 1 or 2 or 3 as per your requirement.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-24
    • 1970-01-01
    相关资源
    最近更新 更多