5

我使用 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();
4

3 回答 3

9

设置OnClickListener显示 ProgressDialog 之后。

DialogAndroid 中可用的其他一些 s 不同,ProgressDialog它没有setNeutralButton()方法,因此您需要在显示onClickListener后设置ProgressDialog,如下所示:

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".

    }

});
于 2014-07-19T18:02:09.753 回答
1

您遇到的问题是 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();
                    }
                }
            });
        }
    });

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

编辑:

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

    final AlertDialog dialog = new AlertDialog.Builder(getActivity())
            .setTitle("Enter Credentials")
            .setView(v)
            .setPositiveButton("OK", null)
            .create();
于 2013-09-02T23:15:43.347 回答
0

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

于 2015-03-26T12:22:28.780 回答