如果字符串不包含某些单词则无法离开对话框

时间:2015-09-07 13:38:20

标签: android dialog alert cancellation

我希望我的代码检查我的对话框中写的内容是否包含某个字符串,如果不能关闭对话框。现在,当我按下okay按钮时,尽管吐司出现了,但对话框仍然消失,所以我猜我做错了setCancelable

             protected void showInputDialog() {
    // get prompts.xml view
    LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
    View promptView = layoutInflater.inflate(R.layout.url, null);

    final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this, AlertDialog.THEME_HOLO_DARK);
    alertDialogBuilder.setView(promptView);

    final EditText editText = (EditText) promptView.findViewById(R.id.Button01);

    alertDialogBuilder.setTitle("Enter URL");
   // alertDialogBuilder.setCustomTitle("Enter URL");
   // alertDialogBuilder.setIcon(R.drawable.icon);

    alertDialogBuilder.setCancelable(false);

    alertDialogBuilder.setPositiveButton("DONE", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            String value = editText.getText().toString();

            if (value.contains("www.facebook.com/")) {
                //Toast.makeText(getBaseContext(), "You didn't enter the Name",Toast.LENGTH_SHORT).show();
               // alertDialogBuilder.setCancelable(false);
            }
            else {
                Toast.makeText(getBaseContext(), "You didn't enter the Name",Toast.LENGTH_SHORT).show();
                alertDialogBuilder.setCancelable(false);
            }
        }
    });
    // create an alert dialog
    AlertDialog alert = alertDialogBuilder.create();
    alert.show();
}

有任何建议如何解决?

修改 我发布了整个功能,虽然我认为它不会帮助你

2 个答案:

答案 0 :(得分:1)

你可以使用onShowListener

dialog.setOnShowListener(new DialogInterface.OnShowListener() {

    @Override
    public void onShow(DialogInterface dialog) {

        Button okBtn = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
        okBtn .setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {

                if (value.contains("www.stackoverflow.com/")) {
                   //do stuff
                   d.dismiss();         
                   }
                   else
                   {
                         Toast.makeText(getBaseContext(), "You didn't enter the Name                   correctly",Toast.LENGTH_SHORT).show();
                        alertDialogBuilder.setCancelable(false);
                       //Don't put dismiss() here 
                   }

            }
        });
    }
});

基于我从你的问题中理解的这应该起作用

答案 1 :(得分:1)

setCancelable无关,final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this, AlertDialog.THEME_HOLO_DARK); alertDialogBuilder.setView(promptView); final EditText editText = (EditText) promptView.findViewById(R.id.Button01); alertDialogBuilder.setTitle("Enter URL"); alertDialogBuilder.setCancelable(false); alertDialogBuilder.setPositiveButton("DONE", null); //part of the workaround // create an alert dialog final AlertDialog alert = alertDialogBuilder.create(); // workaround alert.setOnShowListener(new DialogInterface.OnShowListener() { @Override public void onShow(DialogInterface dialog) { Button okBtn = dialog.getButton(AlertDialog.BUTTON_POSITIVE); okBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (value.contains("www.stackoverflow.com/")) { //you code here alert.dismiss(); } else { Toast.makeText(getBaseContext(), "You didn't enter the Name",Toast.LENGTH_SHORT).show(); //Don't put dismiss() here } } }); } }); alert.show(); 仅禁止用户取消,但不禁用代码取消。

默认情况下,AlertDialog会在任何按钮点击时关闭,但有一种解决方法。

实际上@mothana正在讨论相同的解决方法,我尝试将其与您的代码放在一起。

CreateWindow

您可以在此处查看参考资料 How to prevent a dialog from closing when a button is clicked