Android多个对话框到一个OnClickListener

时间:2016-06-29 11:34:47

标签: android switch-statement android-alertdialog

是否可以将一个OnClickListener分配给一个AlertDialog

以下是对话框本身:

        AlertDialog.Builder dialog1 = new AlertDialog.Builder(PrefActivity.this);
        dialog1.setTitle("Dialog 1");
        dialog1.setPositiveButton("ok" , myListener);

        AlertDialog.Builder dialog2 = new AlertDialog.Builder(PrefActivity.this);
        dialog2.setTitle("Dialog 2");
        dialog2.setPositiveButton("ok" , myListener);

这是听众:

        DialogInterface.OnClickListener myListener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (???){
                    case dialog1:
                    break;

                    case dialog2:
                    break;
                }
            }
        };

我应该在switch-case语句中写什么来正确确定选择了哪个对话框?

2 个答案:

答案 0 :(得分:1)

不要直接显示对话框,构建对话框并将其分配给变量,如:

> version()
3.0.6
> db.doc.insert({tags: [ "A", "B", "C" ] })
WriteResult({ "nInserted" : 1 })
> db.doc.find({tags : {$eq:"A"}})
Error: error: { "$err" : "invalid operator: $eq", "code" : 10068 }

设置相同的列表器

dialog1 = new AlertDialog.Builder(PrefActivity.this);
dialog1 = builder1.create();
dialog2 = new AlertDialog.Builder(PrefActivity.this);
dialog2 = builder2.create();

答案 1 :(得分:-1)

1.简单地创建一个新类,即myListener并实现DialogInterface.OnClickListener接口

2.使用int参数

创建构造函数

3.通过引用新类而不是接口并传递int参数,稍后将在switch statemnt中使用,即dialog.setPositiveButton("OK",new myListener(int))

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
static int dialog1 = 1;
static int dialog2 = 2;
Button button, button2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button = (Button) findViewById(R.id.button);
    button2 = (Button) findViewById(R.id.button2);

    button.setOnClickListener(this);
    button2.setOnClickListener(this);
}

@Override
public void onClick(View view) {
    if (view == button) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Dialog 1");
        builder.setMessage("this is first dialog");
        builder.setPositiveButton("ok", new myListener(dialog1));
        builder.show();
    }
    if (view == button2) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Dialog 2");
        builder.setMessage("this is second dialog");
        builder.setPositiveButton("ok", new myListener(dialog2));
        builder.show();
    }
}


class myListener implements DialogInterface.OnClickListener {
    int dialogNo;

    public myListener(int dialogNo) {
        this.dialogNo = dialogNo;


    }

    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
        switch (dialogNo) {
            case 1:
                Toast.makeText(MainActivity.this, "Dialog first", Toast.LENGTH_SHORT).show();
                break;
            case 2:
                Toast.makeText(MainActivity.this, "Dialog second ", Toast.LENGTH_SHORT).show();
                break;
        }


    }
}
}