主題有點說這一切..我要求從用戶的PIN碼,如果他們進入它,點擊確定正面按鈕和PIN不正確我想顯示一個敬酒,但保持對話框打開。此刻它會自動關閉..當然,這是非常微不足道的事情,但無法找到答案。如何在點擊按鈕onclick後保持alertdialog打開?
謝謝..
主題有點說這一切..我要求從用戶的PIN碼,如果他們進入它,點擊確定正面按鈕和PIN不正確我想顯示一個敬酒,但保持對話框打開。此刻它會自動關閉..當然,這是非常微不足道的事情,但無法找到答案。如何在點擊按鈕onclick後保持alertdialog打開?
謝謝..
構建與屬性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();
非常感謝,所以對話框除非指定了自定義佈局,否則會自動關閉onclick? – Dave 2010-10-25 17:20:28
我確實相信,我可能會誤解,但是我發現自定義對話更容易,而不是嘗試讓另一個人工作,並且完全控制它的外觀和行爲。 Plz將答案標記爲接受,如果它適合你。 – blindstuff 2010-10-25 17:26:27
只要繼續使用你已經擁有的對話框,只是把一個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>
您可以設置一個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();
}
}
});
}
}
你並不需要創建一個自定義類。您可以爲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#
同樣的問題,我在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!
}
});
,其中的觀點是分配給對話框的看法!
試試這個:
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
希望這有助於!祝你好運!
我目前只是回顧我的功能來創建並顯示一個對話框,但它感覺像是浪費資源,當我只需要通知對話框不要自行消除... – Dave 2010-10-25 16:11:26
重複http://stackoverflow.com/questions/2620444 /如何防止對話框從關閉時點擊按鈕/ 9523257 – ccpizza 2012-12-29 13:43:36