主题有点说明了所有..我正在向用户请求PIN码,如果他们输入了PIN码,请单击"确定"按钮并且PIN码不正确我想显示Toast但保持对话框打开.此刻它自动关闭..当然这是非常微不足道的事情要纠正但尚未找到答案.
谢谢..
zen*_*ttu 41
您不需要创建自定义类.您可以为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;
}
}
});
Run Code Online (Sandbox Code Playgroud)
此解决方案的一部分由http://groups.google.com/group/android-developers/browse_thread/thread/fb56c8721b850124#提供
bli*_*uff 13
使用具有属性android:password ="true"按钮的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>
Run Code Online (Sandbox Code Playgroud)
然后当你想要它弹出时:
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();
Run Code Online (Sandbox Code Playgroud)
您可以按如下方式设置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();
}
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
30958 次 |
最近记录: |