gpt4 book ai didi

android - 一次设置多个按钮的 setonlongclicklistener

转载 作者:行者123 更新时间:2023-11-29 18:02:14 27 4
gpt4 key购买 nike

我希望按钮 1、2、3 响应长按,提示用户输入文本按钮。我在 onCreate 中写道:

Button botonEditable;
Context context;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
context = MainActivity.this;
Resources r = getResources();
String pName = getPackageName();

for (int i=1;i<4;i++){
botonEditable = (Button) findViewById(r.getIdentifier("button" + i, "id", pName));
botonEditable.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
final AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setMessage("Nueva Categoria:");
final EditText input = new EditText(context);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton ) {
// Do something with value!
String newCateg = input.getText().toString();
botonEditable.setText(newCateg);
}
});

alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
return true;
}
});
}
}

测试时,3 个按钮相应地响应以显示警告消息,但是当我输入文本并单击确定时,文本仅在按钮 3 中更改,无论哪个按钮被长按 :(出了什么问题以及如何以简单的方式解决它?

最佳答案

试试这个

button.setOnLongClickListener(new longClcik());


class longClcik implements OnLongClickListener {

public boolean onLongClick(View v) {
return false;

}
}

关于android - 一次设置多个按钮的 setonlongclicklistener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15332991/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com