Android:减少处理AlertDialog的代码

时间:2011-03-31 13:43:01

标签: android alertdialog

我是java开发的新手......我正在尝试使用以下代码在Android应用中实现Alert ...

        AlertDialog.Builder alert=new AlertDialog.Builder(this);
        alert.setMessage("I'm a multi-button alert :-)");
        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), 
                    "OK",
                    Toast.LENGTH_LONG)
                    .show();
            }
        });
        alert.setNegativeButton("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), 
                    "KO",
                    Toast.LENGTH_LONG)
                    .show();
            }
        });
        alert.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), 
                    "CANCEL",
                    Toast.LENGTH_LONG)
                    .show();
            }
        });
        alert.show();

它运行,但我想避免,对于每个按钮,新的DialogInterface.OnClickListener ...通过指向处理单击按钮的单个函数。我认为这是可能的,但我不知道怎么样,有人可以帮助我吗?

提前致谢 角

8 个答案:

答案 0 :(得分:11)

您可以在包含的类中实现DialogInterface.OnClickListener并监视which参数以查看,单击了哪个按钮。

alert.setPositiveButton("Ok", this);
alert.setNegativeButton("No", this);
alert.setNeutralButton("Cancel", this);

public void onClick(DialogInterface dialog, int which) {
    String text = "";

    switch (which)
    {
        case DialogInterface.BUTTON_NEGATIVE:
            text = "Cancel";
    }

    Toast.makeText(getApplicationContext(), 
        text,
        Toast.LENGTH_LONG)
        .show();
}

答案 1 :(得分:2)

略有不同的方法:

final Context context = getApplicationContext(); // Should be "final", won't compile otherwise
class MyListener implements DialogInterface.OnClickListener {
    MyListener() {
    }
    public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(context, 
                which == DialogInterface.BUTTON_POSITIVE ? "OK" :
                which == DialogInterface.BUTTON_NEGATIVE ? "KO" :
                "CANCEL",
                Toast.LENGTH_LONG)
                .show();
        }

}
MyListener listener = new MyListener();

AlertDialog.Builder alert=new AlertDialog.Builder(this);
alert.setMessage("I'm a multi-button alert :-)");
alert.setPositiveButton("Ok", listener);
alert.setPositiveButton("No", listener);
alert.setPositiveButton("Cancel", listener);

答案 2 :(得分:1)

您可以为自己定义onClickListener并将其添加到按钮,然后您必须在监听器中确定哪个Button正在调用。这并没有真正减少代码量,但我更喜欢这种方式,因为更好的可读性(?不确定这个词是否存在^^)

答案 3 :(得分:1)

只需实现一个OnClickListener并设置它。

class MyActivity implements DialogInterface.OnClickListener {
    public void onClick(DialogInterface dialog, int which) {
        switch(which) {
            case DialogInterface.BUTTON_POSITIVE:
                Toast.makeText(getApplicationContext(), "OK",Toast.LENGTH_LONG).show();
                break;
            case DialogInterface.BUTTON_NEGATIVE:
                Toast.makeText(getApplicationContext(), "KO",Toast.LENGTH_LONG).show();
                break;
            case DialogInterface.BUTTON_NEUTRAL:
                Toast.makeText(getApplicationContext(), "CANCEL",Toast.LENGTH_LONG).show();
                break;
    }
}

然后

alert.setPositiveButton("Ok", this);
alert.setNegativeButton("No", this);
alert.setNeutralButton("Cancel", this);
在AlertDialog.Builder上。

答案 4 :(得分:1)

已经有一些好的答案,但我想我会添加另一个变体:

public class MyClickListener implements DialogInterface.OnClickListener {

    String mDisplayText;
    Context mCtx;
    public MyClickListener(String displayText, Context ctx){
        mDisplayText = displayText;
        mCtx = ctx;
    }
    public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(mCtx, 
            mDisplayText,
            Toast.LENGTH_LONG)
            .show();
    }
}

...

AlertDialog.Builder alert=new AlertDialog.Builder(this);
alert.setMessage("I'm a multi-button alert :-)");
alert.setPositiveButton("Ok", new MyClickListener("OK",this));
alert.setNegativeButton("No", new MyClickListener("KO",this));
alert.setNeutralButton("Cancel", new MyClickListener("CANCEL",this));

答案 5 :(得分:1)

Chris ...我刚学到的一个提示是你不需要在内部类中调用getBaseContext或getApplicationContext。您可以使用MyActivity.this,如:

    builder.setNegativeButton("Dismiss",  new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            //cancels itself?
           /* Toast.makeText(ConfuseText.this, 
                    "HELLO",
                    Toast.LENGTH_LONG)
                    .show();*/
        }    

我将接受你的说法,你是Android开发的新手。首先,有三种方法可以处理点击事件。匿名内部类(API 3),实现onClickListener(API 3)和XML属性(API 4)。恕我直言的内部类更“面向对象”,onClickListener可能更容易阅读。

其次,考虑使用标准的Android架构来显示AlertDialogs。此策略允许Android操作系统为您处理电话方向更改。这可能是最初的工作,但它会在以后得到回报。所以你需要阅读onCreateDialog。

protected Dialog onCreateDialog(int id) {
    Dialog dialog;
    switch(id) {
    case DIALOG_ABOUT:
        // do the work to define the About Dialog
        dialog= getInstanceAlertDialog();
        break;
    default:
        dialog = null;
        break;
    }
    return dialog;
}

private AlertDialog getInstanceAlertDialog() {
    AlertDialog.Builder builder= new AlertDialog.Builder(this);
    builder.setMessage("Confuse Text JALComputing Copyright 2011");
    AlertDialog alert= builder.create();
    alert.setTitle("About");
    return alert;
}

第三,考虑让事件处理程序尽可能接近AlertDialog代码,就像你正在做的那样。想象一下,如果您决定支持多个警报对话框。除非你有一个巨大的大脑,保持方法和数据接近“对象”不太容易出错

答案 6 :(得分:0)

d.setPositiveButton("Yes",new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
        MainActivity.this.finish();
    }
});

d.setNegativeButton("No",new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
        dialog.cancel();
    }
});

d.setNeutralButton("OK", new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(getBaseContext(), "OK", Toast.LENGTH_LONG).show();
    }
});

答案 7 :(得分:0)

builder = new AlertDialog.Builder(this);
    ListAdapter adapter = new ActionAdapter(this,
            R.layout.spinner_row_comman, arraylist_date, builder);

    builder.setTitle("Select days to set Reminder");
    builder.setAdapter(adapter, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            if (which == 0) {


            } else if (which == 1) {
                dayDate = 2;
                Log.e("dayDate", "++++++++++++++++" + dayDate);

                updateLabel();

            } else if (which == 2) {


            } else if (which == 3) {


            } else if (which == 4) {


            } else if (which == 5) {


            } 
        }

    });

    builder.show();