AlertDialog.Builder不会停留在屏幕上

时间:2013-08-24 14:33:54

标签: android android-activity

在我的应用程序中,我正在显示AlertDialog.Builder

AlertDialog.Builder alert = new AlertDialog.Builder(this);

并且在此警报中有一个EditText和PositiveButton。

AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setMessage("Enter Text");

final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int whichButton) {
if(input.getTest().getText().toString().equalsIgnoreCase("")){
Toast.makeText(Activity.this, "Please enter some text", Toast.LENGTH_LONG).show();
}
}
alert.show();

如果未输入任何内容,则会在吐司上显示请输入一些文字,对话框已关闭,但我希望关闭该“警告”对话框。我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:0)

您需要先创建对话框然后再显示。

            AlertDialog.Builder alert = new AlertDialog.Builder(this);
            AlertDialog alertDialog = alert.create();
            alertDialog.show();

答案 1 :(得分:0)

AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setMessage("Enter Text");

final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog, int whichButton) {}
}

final AlertDialog dialog = alert.create();
dialog.show();

dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
      if(input.getTest().getText().toString().equalsIgnoreCase("")){
      Toast.makeText(Activity.this, "Please enter some text", Toast.LENGTH_LONG).show();
}
}
});