2015-09-07 66 views
0

我想我的代碼來檢查我的對話框中寫入的內容是否包含某個字符串,如果不是無法關閉對話框。現在,當我按下按鈕,沒關係的對話框消失,儘管出現了敬酒的事實,所以我想我已經做了一些錯誤的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(); 
} 

任何建議如何解決呢?

編輯: 我已經發布了全功能的,雖然我不認爲它會幫助你

+0

您發佈不夠代碼,上傳包含該代碼段的整體功能? –

+0

@Derek Fung我發佈了整個代碼,請看看 –

回答

1

u可以使用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 
        } 

      } 
     }); 
    } 
}); 

基於我從你的問題這應該理解工作

+0

我的問題是'alertDialogBu​​ilder.setCancelable(false);'不能工作,這意味着對話不應該在什麼時候解散 –

1

它與setCancelable不相關,只允許用戶取消取消,但不禁用代碼取消。

默認情況下,AlertDialog將在任何按鈕單擊時關閉,但有一種解決方法。

其實@mothana正在談論相同的解決方法,我嘗試將它與您的代碼放在一起。

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

您可以點擊這裏參考 How to prevent a dialog from closing when a button is clicked

相關問題