簡體   English   中英

阻止ProgressDialog被onClick解雇

[英]Prevent ProgressDialog from getting dismissed by onClick

我使用ProgressDialog向用戶顯示他必須等待並且在用戶必須等待時使我的應用程序的表面“不可觸及”。 我在progressDialog中添加了一個Button,如果某些條件為真,它應該啟動一些操作。 問題是,每次用戶按下按鈕時,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();

顯示ProgressDialog后設置OnClickListener

不像其他的一些Dialog的可用Android中, ProgressDialog沒有setNeutralButton()方法,所以你需要設置onClickListenerProgressDialog已經證明,像這樣:

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

    }

});

您遇到的問題是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();

添加connectingDialog.setCancelable(false); 在Android 4.2.2上對我來說足夠了

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM