0
pbutton.setOnClickListener(new OnClickListener()
    {   private AlertDialog show;
        public void onClick(View arg0)
        {   
            if ((input1.getText().length() == 0) || (input1.getText().toString().equals(" ")) || (input2.getText().length() == 0) || (input2.getText().toString().equals(" "))|| (input1.getText().toString().equals(""))||(input2.getText().toString().equals("")))
            {

            show = new AlertDialog.Builder(MainActivity.this).setTitle("Error").setMessage("Some inputs are empty").setPositiveButton("OK", null).show();
            }
            double result = new Double(input1.getText().toString())+ new Double(input2.getText().toString());
            output.setText(Double.toString(result));

        }

我也尝试过传递也不起作用的上下文

4

3 回答 3

1

以这种方式使用:-

   b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
              if ((input1.getText().length() == 0) || (input1.getText().toString().equals(" ")) || (input2.getText().length() == 0) || (input2.getText().toString().equals(" "))|| (input1.getText().toString().equals(""))||(input2.getText().toString().equals("")))
                {
            showAlert();
                }
        }
    });
}
private void showAlert() {
    AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
            alertbox.setMessage("Some Inputs are Empty");
            alertbox.setTitle("error"); 
    alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface arg0, int arg1) {

        }
    });

     alertbox.show();

  }
于 2012-10-18T11:02:43.357 回答
1

I think you need to call the create() first like:

show = new AlertDialog.Builder(MainActivity.this).setTitle("Error").setMessage("Some inputs are empty").setPositiveButton("OK", null).create();

show.show();

EDIT:

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setTitle("Error");
alertDialogBuilder.setMessage("Some inputs are empty");
alertDialogBuilder.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog,int id) {
        // if this button is clicked, ....

    }
  });   


AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
于 2012-10-18T11:04:08.290 回答
0

我按原样使用了您的代码并且工作正常。请确保您没有导入

导入 android.content.DialogInterface.OnClickListener;

如果您在侦听器上方导入,那么您必须在下方导入,它不会起作用,

导入 android.view.View.OnClickListener;

那么它肯定会起作用。

于 2012-10-18T11:15:39.863 回答