显示 ProgressDialog 时 onBackPressed() 不会触发

发布于 2024-11-16 01:24:41 字数 213 浏览 4 评论 0原文

我尝试了很多方法来在显示 progressDialog 时触发 backPressed() 事件。但没有一个有效。如果我提供 progDialog.setcancelable(true); 我可以关闭 progressDialog 但仍然不会触发 onBackPressed() 。

I have tried a lot of ways to trigger the backPressed() event when the progressDialog is displayed. But none works. If I provide progDialog.setcancelable(true); I am able to dismiss the progressDialog but still the onBackPressed() doesn't get triggered.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凡尘雨 2024-11-23 01:24:41

当 ProgressDialog 处于活动状态时,如果您按返回键执行您自己的操作,您必须将 setOnCancelListener 设置为 ProgressDialog。
onCancel() 方法示例中编写您的逻辑,您在 onBackPressed() 事件中编写的整个逻辑,您必须在此处编写这些内容。

示例代码

import android.app.Activity;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;

import android.os.Bundle;

import android.view.KeyEvent;
import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

public class CancelProgressDialog extends Activity {

    ProgressDialog myProgressDialog = null;

    @Override
    public void onCreate(Bundle icicle) {

        super.onCreate(icicle);

        /* Create a very simple button */

        Button b = new Button(this);

        this.setContentView(b);

        b.setText("Show ProgressBar...");

        b.setOnClickListener(myProgressBarShower);

    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        System.out.println("...any key is pressed....");
        if (keyCode == KeyEvent.KEYCODE_BACK)
        {
            System.out.println("...BackButton is pressed...");
            if( (myProgressDialog!= null) && myProgressDialog.isShowing()){
                myProgressDialog.dismiss();
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    /** OnClickListener that fakes some work to be done. */

    OnClickListener myProgressBarShower = new OnClickListener() {

        // @Override

        public void onClick(View arg0) {

            // Display an indeterminate Progress-Dialog

            myProgressDialog = ProgressDialog.show(CancelProgressDialog.this,

            "Please wait...", "Doing Extreme Calculations...", true);
            myProgressDialog.setOnCancelListener(new OnCancelListener() {

                public void onCancel(DialogInterface arg0) {
                    // TODO Auto-generated method stub
                    System.out.println("...cancel button is pressed");
//                  perform your task here
                }
            });
            myProgressDialog.setCancelable(true);



        }



    };

}

谢谢
迪帕克

When ProgressDialog is active if yiou press back key to perform yopur own operations you have to set setOnCancelListener to the progressdialog.
write your logic inside onCancel() method example the whole logic that you have written in onBackPressed() event those things you have to write here.

Sample code

import android.app.Activity;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;

import android.os.Bundle;

import android.view.KeyEvent;
import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

public class CancelProgressDialog extends Activity {

    ProgressDialog myProgressDialog = null;

    @Override
    public void onCreate(Bundle icicle) {

        super.onCreate(icicle);

        /* Create a very simple button */

        Button b = new Button(this);

        this.setContentView(b);

        b.setText("Show ProgressBar...");

        b.setOnClickListener(myProgressBarShower);

    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        System.out.println("...any key is pressed....");
        if (keyCode == KeyEvent.KEYCODE_BACK)
        {
            System.out.println("...BackButton is pressed...");
            if( (myProgressDialog!= null) && myProgressDialog.isShowing()){
                myProgressDialog.dismiss();
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    /** OnClickListener that fakes some work to be done. */

    OnClickListener myProgressBarShower = new OnClickListener() {

        // @Override

        public void onClick(View arg0) {

            // Display an indeterminate Progress-Dialog

            myProgressDialog = ProgressDialog.show(CancelProgressDialog.this,

            "Please wait...", "Doing Extreme Calculations...", true);
            myProgressDialog.setOnCancelListener(new OnCancelListener() {

                public void onCancel(DialogInterface arg0) {
                    // TODO Auto-generated method stub
                    System.out.println("...cancel button is pressed");
//                  perform your task here
                }
            });
            myProgressDialog.setCancelable(true);



        }



    };

}

Thanks
Deepak

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文