【问题标题】:Prevent ProgressDialog from getting dismissed by onClick防止 ProgressDialog 被 onClick 解散
【发布时间】:2013-09-05 21:44:21
【问题描述】:

我使用 ProgressDialog 向用户展示他必须等待,并在用户必须等待时使我的应用程序的表面“不可触碰”。我在 progressDialog 中添加了一个按钮,如果某些条件为真,它应该启动一些操作。问题是每次用户按下按钮时,progressDialog 都会自动关闭。即使没有触发任何动作。调用 onClick 时如何防止 progressDialog 被关闭?

谢谢和问候

编辑:

    connectingDialog = new ProgressDialog(this);
    connectingDialog.setCancelable(false);
    connectingDialog.setCanceledOnTouchOutside(false);
    connectingDialog.setButton(DialogInterface.BUTTON_NEUTRAL, "start", new DialogInterface.OnClickListener(){

        @Override
        public void onClick(DialogInterface arg0, int arg1) {
            /*if(isPrepared)
                startGame();*/
            connectingDialog.show(); // does not work, too
        }

    });
    connectingDialog.show();

【问题讨论】:

    标签: android progressdialog onclicklistener android-button android-dialog


    【解决方案1】:

    在显示 ProgressDialog 后设置OnClickListener

    与 Android 中的其他一些 Dialogs 不同,ProgressDialog 没有 setNeutralButton() 方法,因此您需要在显示 ProgressDialog 之后设置 onClickListener,如下所示:

    connectingDialog = new ProgressDialog(this);
    
    connectingDialog.setCancelable(false);
    connectingDialog.setCanceledOnTouchOutside(false);
    
    // Create the button but set the listener to a null object.
    connectingDialog.setButton(DialogInterface.BUTTON_NEUTRAL, "start", 
            (DialogInterface.OnClickListener) null )
    
    // Show the dialog so we can then get the button from the view.
    connectingDialog.show();
    
    // Get the button from the view.
    Button dialogButton = connectingDialog.getButton( DialogInterface.BUTTON_NEUTRAL );
    
    // Set the onClickListener here, in the view.
    dialogButton.setOnClickListener( new View.OnClickListener() {
    
        @Override
        public void onClick ( View view ) {
    
            if (isPrepared) {
                 connectingDialog.dismiss();
                 startGame();
            }
    
            // Dialog will not get dismissed unless "isPrepared".
    
        }
    
    });
    

    【讨论】:

      【解决方案2】:

      在 Android 4.2.2 上添加 connectingDialog.setCancelable(false); 对我来说已经足够了

      【讨论】:

        【解决方案3】:

        您遇到的问题是 ProgressDialog 继承自 Dialog/AlertDialog 并且默认情况下,当您按下对话框上的按钮时,无论它是哪个按钮(正/负等),它们都会被关闭

        解决方法是拦截按钮并覆盖它的侦听器,但您只能在创建对话框后执行此操作。

        可能还有其他方法可以做到这一点,但我已经成功地使用了这种方法。这是代码中的示例:

            dialog.setOnShowListener(new DialogInterface.OnShowListener() {
                @Override
                public void onShow(DialogInterface dialogInterface) {
                    Button b = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
                    b.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            String username = user.getText().toString();
                            String password = pass.getText().toString();
                            if (username != null && username.length() > 0 && password != null && password.length() > 0) {
                                // store credentials in preferences here
                                dialog.dismiss()
                            } else {
                                Toast.makeText(getActivity(), "Username and/or password cannot be blank.", Toast.LENGTH_SHORT).show();
                                user.requestFocus();
                            }
                        }
                    });
                }
            });
        

        如您所见,我使用它是为了在关闭对话框之前验证我的用户是否确实在某些EditText 中输入了某些内容,否则,提示他们输入。

        编辑:

        值得注意的是,您最初需要向按钮传递一个 null OnClickListener

            final AlertDialog dialog = new AlertDialog.Builder(getActivity())
                    .setTitle("Enter Credentials")
                    .setView(v)
                    .setPositiveButton("OK", null)
                    .create();
        

        【讨论】:

        • +1。我也是这样做的。只有一件事:使用android.R.string.ok 而不是“OK”
        • 注明。我知道硬编码字符串是一种不好的做法,我最终会完全替换它们......
        • 但是我不能调用progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "ok", null),因为它被标记为错误..?
        • @user2224350 设置(DialogInterface.OnClickListener) null 而不是null
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-30
        • 2011-09-30
        • 2016-11-04
        • 1970-01-01
        相关资源
        最近更新 更多