防止ProgressDialog在单击事件中被关闭

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();
3个回答

9

在ProgressDialog显示后设置OnClickListener。

与Android中的其他一些Dialog不同,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".

    }

});

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

正如你所看到的,我使用它来验证用户在关闭对话框之前是否已经在某些EditText中输入了内容,否则提示他们进行输入。 编辑: 值得注意的是,初始时需要将按钮传递给null的OnClickListener
    final AlertDialog dialog = new AlertDialog.Builder(getActivity())
            .setTitle("Enter Credentials")
            .setView(v)
            .setPositiveButton("OK", null)
            .create();

1
+1。这也是我所做的方式。只有一件事:使用android.R.string.ok而不是“OK”。 - Enrichman
知道了。我知道硬编码字符串是一种不好的做法,我最终会逐步替换它们... - Karl
但是我无法调用progressDialog.setButton(DialogInterface.BUTTON_POSITIVE,“ok”,null),因为它被标记为错误..? - user2224350
@user2224350 将 (DialogInterface.OnClickListener) null 设置为 null 的替代。 - isabsent

0

在Android 4.2.2上,只需添加connectingDialog.setCancelable(false);就足够了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接