当前位置: 动力学知识库 > 问答 > 编程问答 >

android - My dialog box below does not work, Please correct

问题描述:

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));

}

I've also tried passing the context which also doesn't work

网友答案:

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();
网友答案:

Use In This Way:-

   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();

  }
网友答案:

I used your code as it is and works fine. Please make sure that you are not import

import android.content.DialogInterface.OnClickListener;

if you imported above listener then it will not works you have to import below,

import android.view.View.OnClickListener;

Then it will definitely works.

分享给朋友:
您可能感兴趣的文章:
随机阅读: