我在App的用户点击按钮后创建一个Dialog,所以我的问题是: 我有我的迷你对话菜单,有两个元素“Lux”和“Vigor”。 我想关联Lux和Vigor的不同功能, 这是可能的?我该怎么做?
我的大问题是识别事件“点击Lux或Vigor” 谢谢你的时间, 国王的问候 我发布了我的代码:
private final static int BUTTON_DIOALOG = 2;
final CharSequence[] items ={"Lux", "Vigor"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton immG = (ImageButton)this.findViewById(R.id.imageButton1);
immG.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MainActivity.this.showDialog(BUTTON_DIOALOG);
}
});
}
@Override
protected Dialog onCreateDialog(final int id) {
Dialog dialog = null;
AlertDialog.Builder builder =new AlertDialog.Builder(this);
switch (id){
case BUTTON_DIOALOG:
builder.setTitle("scegli cosa").setItems(items, new
DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
}
return dialog;
}}
答案 0 :(得分:1)
你可以这样做:
final Context ctx = this;
AlertDialog dialog = new AlertDialog.Builder(this).create();
dialog.setIcon(icon);
dialog.setTitle("");
dialog.setMessage("");
dialog.setCancelable(false);
dialog.setButton(DialogInterface.BUTTON_POSITIVE, "LUX", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int buttonId) {
}
});
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Vigor", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int buttonId) {
}
});
dialog.show();
答案 1 :(得分:0)
回调中的参数“which”告诉您单击了哪个按钮...
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final CharSequence[] items ={"Lux", "Vigor"};
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, items[which], Toast.LENGTH_LONG).show();
}
});
builder.create().show();