代码之家  ›  专栏  ›  技术社区  ›  David Brown

如何在按钮onclick被触发后保持alertdialog打开?

  •  27
  • David Brown  · 技术社区  · 14 年前

    这个话题说明了一切。。我正在向用户请求PIN代码,如果他们输入了密码,请单击OK肯定按钮,PIN不正确。我想显示Toast,但保持对话框打开。现在它自动关闭。。当然这是一件很琐碎的事情,但还找不到答案。

    6 回复  |  直到 14 年前
        1
  •  13
  •   Mustafa Berkay Mutlu    9 年前

    使用带有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> 
    

    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
  •  41
  •   howettl    9 年前

    不需要创建自定义类。您可以为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#

        3
  •  5
  •   josias    14 年前

    您可以如下设置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
  •   Samuel    14 年前

    您可以继续使用已有的对话框,只需在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();
    }
    

    现在,要使用这段代码,只需调用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>
    
        5
  •  1
  •   Shreshth Kharbanda    7 年前

    试试这个:

    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


    希望这有帮助!祝你好运!

        6
  •  0
  •   GabrieleG    8 年前

    同样的问题我在一个片段对话框。以下是我的犯罪/优雅解决方案:

    <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!
                    }
                });