2010-10-25 80 views
25

主題有點說這一切..我要求從用戶的PIN碼,如果他們進入它,點擊確定正面按鈕和PIN不正確我想顯示一個敬酒,但保持對話框打開。此刻它會自動關閉..當然,這是非常微不足道的事情,但無法找到答案。如何在點擊按鈕onclick後保持alertdialog打開?

謝謝..

+0

我目前只是回顧我的功能來創建並顯示一個對話框,但它感覺像是浪費資源,當我只需要通知對話框不要自行消除... – Dave 2010-10-25 16:11:26

+2

重複http://stackoverflow.com/questions/2620444 /如何防止對話框從關閉時點擊按鈕/ 9523257 – ccpizza 2012-12-29 13:43:36

回答

12

構建與屬性Android上的EditText一個自定義對話框:密碼=「真」按鈕,然後手動設置的onClick監聽器按鈕,並明確選擇它做什麼。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <EditText 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:minWidth="180dip" 
     android:digits="1234567890" 
     android:maxLength="4" 
     android:password="true"/> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

     <Button 
      android:id="@+id/Accept" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="Accept"/> 

    </LinearLayout> 
</LinearLayout> 

然後,當你希望它彈出:

final Dialog dialog = new Dialog(RealizarPago.this); 
dialog.setContentView(R.layout.custom_dialog); 
dialog.setTitle("PIN number:"); 
dialog.setCancelable(true); 

Button button = (Button) dialog.findViewById(R.id.Accept); 
button.setOnClickListener(new OnClickListener() { 
@Override 
    public void onClick(View v) { 
     if(password_wrong){ 
      // showToast 
     } else{ 
      dialog.dismiss(); 
      // other stuff to do 
     } 
    } 
}); 

dialog.show(); 
+0

非常感謝,所以對話框除非指定了自定義佈局,否則會自動關閉onclick? – Dave 2010-10-25 17:20:28

+0

我確實相信,我可能會誤解,但是我發現自定義對話更容易,而不是嘗試讓另一個人工作,並且完全控制它的外觀和行爲。 Plz將答案標記爲接受,如果它適合你。 – blindstuff 2010-10-25 17:26:27

1

只要繼續使用你已經擁有的對話框,只是把一個if子句中的onClick()說

if(pin_check_method){ //pin_check_method should be a boolean returned method 
    //close the Dialog, then continue 
    } 
    else{ 
    //dont put the dialog.dismiss() in here, put instead 
    Toast.makeText(getApplicationContext(),"Invalid pin, please try again",Toast.LENGTH_LONG).show(); 
} 

現在,要使用此代碼,只需調用text.setText(「」);並把文本,你想在這裏 常見的錯誤是,當你鍵入:

TextView text = (TextView) findViewById(R.id.dialog); 

你錯過,它需要實際上是

dialog.findViewById 

,這是不管的名字是什麼在我的例子中,對話框恰好是同名的。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/layout_root" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       > 

    <TextView android:id="@+id/text" 
       android:layout_height="wrap_content" 
       android:textColor="#FFF" 
       android:layout_centerHorizontal="true" 
       android:layout_width="wrap_content"/> 



    <Button android:text="Continue" 
      android:id="@+id/Button01" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" android:layout_below="@+id/text"> 
      </Button> 

</RelativeLayout> 
+0

這就是我正在做的事情,但是即使沒有dismiss()調用,對話框也會自動關閉。 – Dave 2010-10-25 17:19:51

+0

好吧,看着它後,它看起來像自定義對話框將是你最好的選擇..我會編輯我的答案,你會發現有用的。我實際上是在我的應用程序中使用它。不要忘記標記爲接受,如果你使用我的答案:) – Samuel 2010-10-26 03:52:22

4

您可以設置一個OnClickListener如下,以保持對話框打開:

public class MyDialog extends AlertDialog { 
    public MyDialog(Context context) { 
     super(context); 
     setMessage("Hello"); 
     setButton(AlertDialog.BUTTON_POSITIVE, "Ok", (new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       // this will never be called 
      } 
     }); 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (ok) { 
        // do something 
        dismiss(); 
       } else { 
        Toast.makeText(getContext(), "when you see this message, the dialog should stay open", Toast.LENGTH_SHORT).show(); 
       } 
      } 
     }); 
    } 
} 
36

你並不需要創建一個自定義類。您可以爲AlertDialog註冊一個View.OnClickListener。這個監聽器不會關閉AlertDialog。這裏的訣竅是你需要在顯示對話框後註冊監聽器,但是它可以在OnShowListener中完成。您可以使用附件布爾變量來檢查,如果這已經這樣做了,它只會做一次:這個解決方案的

/* 
    * Prepare the alert with a Builder. 
    */ 
    AlertDialog.Builder b = new AlertDialog.Builder(this); 

    b.setNegativeButton("Button", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) {} 
    }); 
    this.alert = b.create(); 

    /* 
    * Add an OnShowListener to change the OnClickListener on the 
    * first time the alert is shown. Calling getButton() before 
    * the alert is shown will return null. Then use a regular 
    * View.OnClickListener for the button, which will not 
    * dismiss the AlertDialog after it has been called. 
    */ 

    this.alertReady = false; 
    alert.setOnShowListener(new DialogInterface.OnShowListener() { 
     @Override 
     public void onShow(DialogInterface dialog) { 
      if (alertReady == false) { 
       Button button = alert.getButton(DialogInterface.BUTTON_NEGATIVE); 
       button.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         //do something 
        } 
       }); 
       alertReady = true; 
      } 
     } 
    }); 

部分由http://groups.google.com/group/android-developers/browse_thread/thread/fb56c8721b850124#

+0

這個解決方案爲我工作,謝謝 – 2011-10-26 15:43:07

+1

不幸的是,setOnShowListener只適用於API級別8和更高版本。爲了支持API 7,我使用鏈接頁面中提供的方法:將一個無操作監聽器傳遞給setNegativeButton,調用show(),然後獲取對該按鈕的引用,並使用完整監聽器調用setOnClickListener()。 – Travis 2012-02-05 04:57:07

+0

對於好的和有幫助的答案+1,這個解決方案適用於我。 – rajpara 2012-10-03 06:19:01

0

同樣的問題,我在FragmentDialog提供。這是我的犯罪/優雅的解決方案: 刪除對話框中的所有按鈕(正面,負面,中性)。從xml.eg與添加您的按鈕:

<LinearLayout 
     android:layout_width="match_parent" 
     android:orientation="horizontal" 
     android:layout_height="wrap_content"> 
     <Button 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:layout_height="wrap_content" 
      android:id="@+id/button_cancel" 
      style="@style/Widget.AppCompat.Button.Borderless.Colored" 
      android:text="@android:string/cancel" 
      android:layout_gravity="left" 
      /> 
     <Button 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:layout_height="wrap_content" 
      android:id="@+id/button_ok" 
      style="@style/Widget.AppCompat.Button.Borderless.Colored" 
      android:text="@android:string/ok" 
      android:layout_gravity="right" 
      /> 
    </LinearLayout> 

然後在你的代碼處理:

view.findViewById(R.id.button_ok).setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view2) { 
        if (wannaClose) 
         dismiss(); 
        else 
         //do stuff without closing! 
       } 
      }); 

,其中的觀點是分配給對話框的看法!

0

試試這個:

final AlertDialog alertDialog = new AlertDialog.Builder(context) 
     .setView(v) 
     .setTitle(R.string.my_title) 
     .setPositiveButton(android.R.string.ok, null) //Set to null. We override the onclick 
     .setNegativeButton(android.R.string.cancel, null) 
     .create(); 

alertDialog.setOnShowListener(new DialogInterface.OnShowListener() { 

    @Override 
    public void onShow(DialogInterface dialog) { 

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

      @Override 
      public void onClick(View view) { 
       // TODO Do something 

      } 
     }); 
    } 
}); 
alertDialog.show(); 

來源:Prevent Alertdialog from closing after button click


希望這有助於!祝你好運!

相關問題