当触摸屏幕时,进度对话框关闭

16

我在线程中使用了 ProgressDialog。在 onButtonClick 中启动了该线程,但当我触摸屏幕上的任何地方时,ProgressDialog 就会关闭。

我该如何防止这种情况发生?

private void ButtonClick(View view) {

    btn1 = (Button) view.findViewById(R.id.btn1);
    btn1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            GetStudentData();
        }
    });
}

private synchronized void GetStudentData() {  
    try {
        // Thread to display loader
        new Thread() {
            @Override
            public void run() {
                Looper.prepare();
                dialog = Msg.ShowProgressDialogBox(
                    getActivity(),
                    dialog,
                    "Please wait while we fetch the student data...");
                Looper.loop();
            }
        }.start();

        new Thread() {
            @Override
            public void run() {
                String studentData="Kailas";
            }   
        }
    } catch(Exception ex {
    }
}

更新

当我在屏幕上点击时,ProgressDialog 会消失但是数据已经全部获取。


你解决了你的问题吗?你的对话进行得如何? - JJ86
我使用了dialog.setCanceledOnTouchOutside(getRetainInstance());这段代码,它对我来说运行得很好。 - Kailas
4个回答

54

将此内容添加到您的对话框中:

yourDialog.setCanceledOnTouchOutside(false);

以这种方式,您可以触摸屏幕而对话框不会被取消。

编辑

如果您正在使用DialogFragment,则必须在调用show(FragmentManager mng,String tag)之前使用以下代码片段:

更多信息请看此处

dialogFragment.setCancelable(false);

编辑2

要小心使用ProgressDialog,因为在Android Oreo(v8.0)中,它现在已被弃用。


我已经使用了这些代码 yourDialog.setCanceledOnTouchOutside(false); 和 yourDialog.setCancelable(true); 但它们没有起作用。 - Kailas
在 Looper.loop() 之前尝试使用 dialog.setCancelable(false);。 - Adrien Cerdan
@Kailas,你把这行代码放在哪里?我通常在创建对话框时放置此行,而不是在run(){}函数中。 - JJ86
@Kailas 其他地方可能有问题;我宁愿使用AsyncTask,在其中您可以创建一个对话框,并使用onPreExecute()和onPostExecute()显示和关闭它。 - JJ86
非常准确,使用AlertDialog解决了我的问题。已点赞 :) - tony gil

5
private synchronized void GetStudentData() {

    try {
    // Thread to display loader
    new Thread() {
        @Override
        public void run() {
            Looper.prepare();
            dialog = Msg.ShowProgressDialogBox(getActivity(),
                    dialog,
                    "Please wait while we fetch the student data...");
            dialog.setCanceledOnTouchOutside(getRetainInstance());
            Looper.loop();
        }
    }.start();

    new Thread() {

        @Override
        public void run() {
                  String studentData="Kailas";
         }   
    }
}

我在我的Android设备操作系统(4.0.1),以及测试过的操作系统(2.6.3)上使用了这条语句dialog.setCanceledOnTouchOutside(getRetainInstance());,并且它的表现很好。


这段代码将如何理解数据何时被获取?以及使用什么参数关闭对话框? - hadi
1
在第二个线程中,您可以处理您的代码。然后您可以关闭对话框。 - Kailas

3
当您正在进行某些重要数据下载过程时,可以使用以下代码使进度对话框无法取消:
mDialog.setCancelable(false);
mDialog.setCanceledOnTouchOutside(false);
mDialog.setOnCancelListener(new Dialog.OnCancelListener() {

    @Override
    public void onCancel(DialogInterface dialog) {
        // DO SOME STUFF HERE
    }
}

如果您在项目中使用操作栏,还可以选择操作栏中的进度条。希望这会对您有所帮助。

2
制作一个像这样的类:
public class Progresss {

    static ProgressDialog dialog;

    public static void start(Context context) {
        dialog = new ProgressDialog(context);
        try {
            dialog.show();
        } catch (BadTokenException e) {
        }
        dialog.setCancelable(false);
        dialog.setContentView(R.layout.progressdialog123);
        // dialog.setMessage(Message);
        // return dialog;
    }

    public static void stop() {
        if(dialog!=null)
            dialog.dismiss();
    }
}

这是布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@android:color/transparent" >

    <ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

</RelativeLayout>

使用方法如下:

Progresss.start(context);
Progresss.stop();

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