Onclick监听器方法中的警报对话框

时间:2012-03-15 15:49:15

标签: android alertdialog

  rdmrbtn.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                AlertDialog alert = new AlertDialog.Builder(getApplicationContext()).create();
                alert.setTitle("No Internet access");
                alert.setMessage("You can't reach the discussion from the Offline mode");
                alert.setButton("OK", new OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub

                    }
                })
            }
        });

我想在点击更多按钮(“rdmr btn”)时显示上述警告。但它给了我一些编译错误。拜托,请你帮帮我吧。谢谢。

2 个答案:

答案 0 :(得分:11)

尝试

rdmrbtn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {


            AlertDialog alert = new AlertDialog.Builder(YourActivity.this).create();
            alert.setTitle("No Internet access");
            alert.setMessage("You can't reach the discussion from the Offline mode");
            alert.setButton("OK", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub

                }
            });
            alert.show();
        }
    });

答案 1 :(得分:2)

您似乎没有正确设置AlertDialog.Builder类的“Context”参数。 尝试下面,更清洁的方法。另外,在strings.xml文件中定义“ok”。

rdmrbtn.setOnClickListener(new Button.OnClickListener() {    
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setTitle("No Internet access");
                builder.setMessage("You can't reach the discussion from the Offline mode");
                builder.setPositiveButton(R.string.ok, new OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                    }
                })
                AlertDialog alert = builder.create();
                alert.show();
            }
        });