The subject kinda says it all.. I'm requesting a PIN code from the user, if they enter it, click the OK Positive Button and the PIN is incorrect I want to display a Toast but keep the dialog open. At the moment it closes automatically.. Sure this is very trivial thing to correct but can't find the answer yet.
主题有点说明了所有..我正在向用户请求PIN码,如果他们输入了PIN码,请单击“确定”按钮并且PIN码不正确我想显示Toast但保持对话框打开。此刻它自动关闭..当然这是非常微不足道的事情要纠正但尚无法找到答案。
Thanks..
6 个解决方案
#1
12
Build a custom dialog with a EditText with the attribute android:password="true" a button, then manually set onClick listener the button, and explicitly choose what to do in it.
使用具有属性android:password =“true”按钮的EditText构建自定义对话框,然后手动设置onClick listener按钮,并明确选择要在其中执行的操作。
<?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>
Then when you want it to pop up:
然后当你想要它弹出时:
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();
#2
37
You do not need to create a custom class. You can register a View.OnClickListener for the AlertDialog. This listener will not dismiss the AlertDialog. The trick here is that you need to register the listener after the dialog has been shown, but it can neatly be done inside an OnShowListener. You can use an accessory boolean variable to check if this has already been done so that it will only be done once:
您不需要创建自定义类。您可以为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;
}
}
});
Part of this solution was provided by http://groups.google.com/group/android-developers/browse_thread/thread/fb56c8721b850124#
此解决方案的一部分由http://groups.google.com/group/android-developers/browse_thread/thread/fb56c8721b850124#提供
#3
4
You can set an OnClickListener as follows to keep the dialog open:
您可以按如下方式设置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();
}
}
});
}
}
#4
1
You can just continue using the dialog you already have, just put an if clause in the onClick() saying
您可以继续使用已有的对话框,只需在onClick()中添加一个if子句即可
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();
}
Now, to use this code, simply invoke text.setText(""); and put in the text you want here common error is that when you type in:
现在,要使用此代码,只需调用text.setText(“”);并输入你想要的文本常见错误是当你输入:
TextView text = (TextView) findViewById(R.id.dialog);
you miss that it needs to actually be
你想念它真的需要它
dialog.findViewById
and this is regardless of what the name of the dialog is, in my example it just happens to be the same name.
这不管对话框的名称是什么,在我的例子中它恰好是同一个名字。
<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>
#5
0
Same problem for me in a FragmentDialog. Here's my criminal/elegant solution: Remove all buttons from the dialog (positive,negative,neutral). Add your buttons from the xml.eg.:
在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>
And then in your code handle it with:
然后在你的代码中处理它:
view.findViewById(R.id.button_ok).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view2) {
if (wannaClose)
dismiss();
else
//do stuff without closing!
}
});
where view is the view assigned to the dialog!
其中view是分配给对话框的视图!
#6
0
Try this:
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();
Source: Prevent Alertdialog from closing after button click
来源:单击按钮后阻止Alertdialog关闭
Hope This Helps! Good Luck!
希望这可以帮助!祝你好运!
#1
12
Build a custom dialog with a EditText with the attribute android:password="true" a button, then manually set onClick listener the button, and explicitly choose what to do in it.
使用具有属性android:password =“true”按钮的EditText构建自定义对话框,然后手动设置onClick listener按钮,并明确选择要在其中执行的操作。
<?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>
Then when you want it to pop up:
然后当你想要它弹出时:
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();
#2
37
You do not need to create a custom class. You can register a View.OnClickListener for the AlertDialog. This listener will not dismiss the AlertDialog. The trick here is that you need to register the listener after the dialog has been shown, but it can neatly be done inside an OnShowListener. You can use an accessory boolean variable to check if this has already been done so that it will only be done once:
您不需要创建自定义类。您可以为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;
}
}
});
Part of this solution was provided by http://groups.google.com/group/android-developers/browse_thread/thread/fb56c8721b850124#
此解决方案的一部分由http://groups.google.com/group/android-developers/browse_thread/thread/fb56c8721b850124#提供
#3
4
You can set an OnClickListener as follows to keep the dialog open:
您可以按如下方式设置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();
}
}
});
}
}
#4
1
You can just continue using the dialog you already have, just put an if clause in the onClick() saying
您可以继续使用已有的对话框,只需在onClick()中添加一个if子句即可
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();
}
Now, to use this code, simply invoke text.setText(""); and put in the text you want here common error is that when you type in:
现在,要使用此代码,只需调用text.setText(“”);并输入你想要的文本常见错误是当你输入:
TextView text = (TextView) findViewById(R.id.dialog);
you miss that it needs to actually be
你想念它真的需要它
dialog.findViewById
and this is regardless of what the name of the dialog is, in my example it just happens to be the same name.
这不管对话框的名称是什么,在我的例子中它恰好是同一个名字。
<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>
#5
0
Same problem for me in a FragmentDialog. Here's my criminal/elegant solution: Remove all buttons from the dialog (positive,negative,neutral). Add your buttons from the xml.eg.:
在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>
And then in your code handle it with:
然后在你的代码中处理它:
view.findViewById(R.id.button_ok).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view2) {
if (wannaClose)
dismiss();
else
//do stuff without closing!
}
});
where view is the view assigned to the dialog!
其中view是分配给对话框的视图!
#6
0
Try this:
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();
Source: Prevent Alertdialog from closing after button click
来源:单击按钮后阻止Alertdialog关闭
Hope This Helps! Good Luck!
希望这可以帮助!祝你好运!