我希望 button1,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 个按钮会相应响应以显示警告消息,但是当我输入文本并单击确定时,文本仅在 button3 中更改,无论长按哪个按钮:( 出了什么问题以及如何修复它一个简单的方法?