android - 防止 ProgressDialog 被 onClick 解雇

标签 android progressdialog onclicklistener android-button android-dialog

我使用 ProgressDialog 向用户显示他必须等待,并在用户必须等待时使我的应用程序表面“不可触摸”。我向 progressDialog 添加了一个按钮,如果某些条件为真,它应该开始一些操作。问题是每次用户按下按钮时,progressDialog 都会自动消失。即使没有触发任何 Action 。如何防止在调用 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();

最佳答案

在 ProgressDialog 显示后设置 OnClickListener

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

    }

});

关于android - 防止 ProgressDialog 被 onClick 解雇,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18579030/

相关文章:

android - REST 和 ListView 刷新

android - Activity生命周期方法: onPostResume significance

android - ProgressDialog - 如何删除数字

android - 动态表格布局上的 OnClickListener

android - 自定义按钮的 ClickListener

android - 超出最小未出列缓冲区计数

具有线程问题的Android ProgressDialog

Android 进度对话框从类文件中设置消息

android - RecyclerView onClickListener 设置性能

android - 使用替换时 FragmentManager 崩溃