【问题标题】:Using setOnLongClickListener to setText on multiples of buttons使用 setOnLongClickListener 在多个按钮上设置文本
【发布时间】:2021-03-03 21:30:31
【问题描述】:

我试图在 setOnLongClickListener 之后更改按钮上的文本,并且有六个按钮可供选择。目前,无论我点击哪个按钮,列表按钮都会更新为新文本。

我想我已经尝试了这个线程上的所有内容: setonlongclicklistener for several buttons at once

最终我希望将这些新按钮值保存到共享首选项中,以便下次启动应用程序时它们就在那里。

public class MainActivity extends AppCompatActivity {

    private Button btn;
    Context context;
    final String[] task = new String[1];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        context = MainActivity.this;
        Resources r = getResources();
        String pName = getPackageName();

        String PREFERENCES_FILE_KEY = "com.example.buttondemo";
        String SECURE_PREFS_FILE_KEY = "com.example.buttonnames";

        // Declare shared preferences
        SharedPreferences sharedPreferences = this.getSharedPreferences(PREFERENCES_FILE_KEY, Context.MODE_PRIVATE);

        // get shared preferences for each button and apply the stored name to each button
        //String buttonText = sharedPreferences.getString("Value01", "Button_01");

        for (int i=1;i<=6;i++) {
            String buttonId = "button" + i;
            btn = (Button) findViewById(r.getIdentifier(buttonId, "id", pName));
            btn.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(final View v) {

                    task[0] = showAddItemDialog(MainActivity.this, btn);
                    //sharedPreferences.edit().putString(buttonId, task[0]).apply();
                    return true;
                }
            });
        }
    }


    private String showAddItemDialog(Context context, Button btnNew) {
        final EditText taskEditText = new EditText(context);
        taskEditText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(10)});
        final String[] task = new String[1];
        AlertDialog dialog = new AlertDialog.Builder(context)
                .setTitle("Enter New button value")
                .setMessage("Enter New button value:")
                .setView(taskEditText)
                .setPositiveButton("Update", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        task[0] = String.valueOf(taskEditText.getText());
                        btnNew.setText(String.valueOf(taskEditText.getText()));
                    }
                })
                .setNegativeButton("Cancel", null)
                .create();
        dialog.show();

        return task[0];
    }
}

谢谢。

【问题讨论】:

  • 我是否认为无论您按下哪个按钮,文本都会在第 6 个按钮上发生变化?
  • 是的,没错。

标签: java android mobile-application


【解决方案1】:

发生的情况是 btn 变量在循环的每次迭代中都被重新分配。因此,一旦长点击侦听器触发,您将调用showAddItemDialog(this, btn),其中btn 持有对上一次循环迭代中设置的任何内容的引用(当i = 6 时)。

所以你遇到的行为是有道理的。希望这足以为您指明正确的方向。


附带说明一下,根据来自 r.getIdentifier() 的动态 ID 查找视图可能是一个糟糕的设计选择,并且可能会在未来引发错误。如果可能,我建议将其简化为仅使用 R.id.button1R.id.button2 等。

【讨论】:

  • 我感觉这就是问题的原因。那么我要在 onLongClickListener 内移动循环吗?我是这样做的,因为会有 15 个按钮,并且有 15 次调用 sharedPreferenes 来保存这些值。所以它可能会变得很粗。
  • 您的循环在概念上很好,您只需要将 btn 作为局部变量而不是您的类的成员。
  • 完美,谢谢。按钮 btn = (Button) findViewById(r.getIdentifier(buttonId, "id", pName));我将尝试这个循环解决方案和单个 R.id.button1、R.id.button2 来看看它是如何读取的。
猜你喜欢
  • 2013-02-26
  • 1970-01-01
  • 2022-01-12
  • 2013-11-20
  • 1970-01-01
  • 2012-11-08
  • 2016-03-28
  • 2012-02-18
  • 1970-01-01
相关资源
最近更新 更多