如果EditText为空,则禁用关闭AlertDialog

时间:2015-06-12 09:10:15

标签: android alertdialog

我有EditText的警告对话框。如果用户按下正按钮且EditText为空,则关闭警报对话框。现在我想要禁用关闭,如果用户将编辑文本留空。怎么做?

这是警告对话框的代码:

@Override
        public void onClick(View v) {

            AlertDialog.Builder wndInput = new AlertDialog.Builder(
                    MainActivity.this);

            final EditText txtEditScraps = new EditText(MainActivity.this);
            txtEditScraps.setInputType(InputType.TYPE_CLASS_PHONE);
            wndInput.setTitle("Number of scraps:");
            wndInput.setCancelable(false);


            wndInput.setNegativeButton("CANCEL",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });

            wndInput.setPositiveButton("OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {                                                                                                                                             
                        }
                    });

            wndInput.setView(txtEditScraps);
            wndInput.create().show();

        }

5 个答案:

答案 0 :(得分:6)

首先对代码的最后两行做一些小改动。

wndInput.setView(txtEditScraps);
final AlertDialog alertDialog = wndInput.create();
alertDialog.show();

表示您只是引用了警告对话框..然后添加以下代码。

alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
          Boolean wantToCloseDialog = (txtEditScraps.getText().toString().trim().isEmpty());
         // if EditText is empty disable closing on possitive button    
         if (!wantToCloseDialog)     
              alertDialog.dismiss();
      }
});

答案 1 :(得分:3)

试试这个

  final AlertDialog alertDialog = new AlertDialog.Builder(this)
                .setTitle("My dialog")
                .setPositiveButton(android.R.string.ok, null)
                .setNegativeButton(android.R.string.cancel, null)
                .create();

        alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {

            @Override
            public void onShow(DialogInterface dialog) {

                Button button = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
                button.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        Toast.makeText(MainActivity.this, "Now dialog will not close", Toast.LENGTH_LONG).show();
                        //alertDialog.dismiss();
                    }
                });
            }
        });
        alertDialog.show();

答案 2 :(得分:1)

你可以像这样查看onClick()方法中的条件:

 wndInput.setPositiveButton("OK",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {  

                       String etContent=txtEditScraps.getText();

                       if(!etContent.isEmpty()){

                                 wndInput.dismiss();
                                  }   
                        else{// do nothing or give editText is empty}                                                                                                                                        
                            }
                        });

答案 3 :(得分:1)

如果EditText值的长度为null,则需要获取EditText的文字,然后停用setPositiveButton事件。

你可以这样做。

   @Override
        public void onClick(View v) {

            AlertDialog.Builder wndInput = new AlertDialog.Builder(
                    MainActivity.this);

            final EditText txtEditScraps = new EditText(MainActivity.this);
            txtEditScraps.setInputType(InputType.TYPE_CLASS_PHONE);
            wndInput.setTitle("Number of scraps:");
            wndInput.setCancelable(false);


            wndInput.setNegativeButton("CANCEL",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });

            wndInput.setPositiveButton("OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {        
                            String _edtValue = txtEditScraps.getText().toString().trim();
                            if (_edtValue.length()==0&&_edtValue==null&&_edtValue.equalsIgnoreCase("")) {
                                //do nothing.
                            }  else {
                                // dismiss your alert.
                            }
                        }
                    });

            wndInput.setView(txtEditScraps);
            wndInput.create().show();

        }

答案 4 :(得分:0)

@Override
public void onClick(View v) {

    AlertDialog.Builder wndInput = new AlertDialog.Builder(
            MainActivity.this);

    final EditText txtEditScraps = new EditText(MainActivity.this);
    txtEditScraps.setInputType(InputType.TYPE_CLASS_PHONE);
    wndInput.setTitle("Number of scraps:");
    wndInput.setCancelable(false);


    wndInput.setNegativeButton("CANCEL",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                 wndInput.dismiss();
                }
            });

    wndInput.setPositiveButton("OK",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                if(txtEditScraps.getText().toString().trim()=="")    {
                    txtEditScraps.requestFocus(true);
                }
                else {
                     wndInput.dismiss();
                }
                }
            });

    wndInput.setView(txtEditScraps);
    wndInput.create().show();

}