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));
}
我也试过传递也不起作用的上下文
答案 0 :(得分: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();
}
答案 1 :(得分:1)
我认为你需要先调用create()
,如:
show = new AlertDialog.Builder(MainActivity.this).setTitle("Error").setMessage("Some inputs are empty").setPositiveButton("OK", null).create();
show.show();
修改强>:
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();
答案 2 :(得分:0)
我按原样使用了您的代码并且工作正常。 请确保您不是导入
导入android.content.DialogInterface.OnClickListener;
如果您导入上面的侦听器,那么它将无效 你必须在下面导入,
导入android.view.View.OnClickListener;
然后肯定会有效。