【问题标题】:Progress Dialog is closed when touch on screen触摸屏幕时关闭进度对话框
【发布时间】:2013-11-08 11:02:34
【问题描述】:

我在线程中使用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 会消失,但会获取所有数据。

【问题讨论】:

  • 您的问题解决了吗?你的对话进展如何?
  • 我使用了 dialog.setCanceledOnTouchOutside(getRetainInstance());这些代码对我来说很好用。

标签: android progressdialog android-progressbar


【解决方案1】:

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

yourDialog.setCanceledOnTouchOutside(false);

这样你就可以触屏了,Dialog不会被取消。

编辑

如果你使用DialogFragment,那么在调用show(FragmentManager mng, String tag)之前必须使用下面的代码sn-p

更多信息here

dialogFragment.setCancelable(false);

编辑 2

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

【讨论】:

  • 我已经使用了这些 yourDialog.setCanceledOnTouchOutside(false);和 yourDialog.setCancelable(true);但它不起作用
  • 尝试 dialog.setCancelable(false); Looper.loop() 之前;
  • @Kailas 你把线放在哪里?我总是在创建对话框时放置这一行,而不是在 run(){} 函数上。
  • @Kailas 其他地方有问题;我宁愿使用 AsyncTask,您可以在其中创建一个 Dialog,还可以使用 onPreExecute() 和 onPostExecute() 来显示()和解除()它。
  • 发现问题,使用AlertDialog 解决了我的问题。赞成:)
【解决方案2】:
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";
         }   
    }
}

我已经使用了这一行 dialog.setCanceledOnTouchOutside(getRetainInstance()); 并且它在我的 android 设备 OS(4.0.1) 中运行良好,也在 OS(2.6.3) 上进行了测试

【讨论】:

  • 这段代码如何理解何时获取数据?并用什么参数关闭对话框?
  • 在您处理代码的第二个线程中。然后你可以关闭对话框。
【解决方案3】:

当您进行某些重要数据下载过程时,您可以使用以下代码使进度对话框无法取消:

mDialog.setCancelable(false);
mDialog.setCanceledOnTouchOutside(false);
mDialog.setOnCancelListener(new Dialog.OnCancelListener() {

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

如果您在项目中使用,也可以在操作栏中查看进度条。

希望对你有所帮助。

【讨论】:

    【解决方案4】:

    创建一个这样的类:

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

    【讨论】:

      猜你喜欢
      • 2014-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-09
      • 1970-01-01
      相关资源
      最近更新 更多