Android 如何防止用户在延迟期间使用处理程序执行任何操作?

Android 如何防止用户在延迟期间使用处理程序执行任何操作?,android,android-handler,Android,Android Handler,我知道这个问题可能不够清楚。是否有一种方法可以防止用户在延迟(3秒)期间使用处理程序在屏幕上单击或执行任何操作我认为最好在这3秒钟内使用setCancelable(false)显示对话框(等待对话框…)。防止这种情况的最佳方法是禁用所有可触摸事件。您可以将此标志与检查事件相结合 要阻止的代码: getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, WindowManager.LayoutParams.FLAG_NO

我知道这个问题可能不够清楚。是否有一种方法可以防止用户在延迟(3秒)期间使用
处理程序在屏幕上单击或执行任何操作
我认为最好在这3秒钟内使用setCancelable(false)显示对话框(等待对话框…)。

防止这种情况的最佳方法是禁用所有可触摸事件。您可以将此标志与检查事件相结合

要阻止的代码:

getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
要取消阻止(清除该标志):


您必须使用带有加载的对话框,该对话框将让用户知道正在进行某些操作

  private void showLoading(String msg) {
        dialog = new Dialog(this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.loading);
        TextView msgtv = (TextView) dialog.findViewById(R.id.msg);
        msgtv.setVisibility(View.VISIBLE);
        msgtv.setText(msg);
        if (msg.isEmpty()) {
            msgtv.setVisibility(View.GONE);
        }
        dialog.setCancelable(false);

        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(this, R.color.alpha_transparent)));

        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        dialog.getWindow().setAttributes(lp);

        dialog.show();

        // this runnable will hide dialog after sometime if networkresponse is slow.
        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                LogManager.i(TAG, "run");
                if (dialog != null && dialog.isShowing()) {
                    LogManager.i(TAG, "run dialog");

                    //     if (status_network_connection) {
                    if (isNetworkConnected()) {

                    } else {
                        showNetworkError();

                    }
                    hideCustomDialog();
                    //  }


                }


            }
        }, 3000);
    }
使用下面的代码隐藏对话框

// hide loading dialog
public void hideCustomDialog() {
    if (dialog != null) {
        if (dialog.isShowing()) {
            dialog.dismiss();

        }
    }
    dialog = null;
}
创建一个xml文件
load.xml
,该文件将定义加载视图

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/progres"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@drawable/progress_bg"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="@dimen/inner_img_xl">

    <ProgressBar
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:indeterminate="true" />

    <TextView
        android:id="@+id/msg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/inner_img_m"
        android:textColor="@color/colorDividerGray"
        android:textSize="@dimen/text_size_button_s"
        android:visibility="gone" />
</LinearLayout>


阻止用户执行任何操作的最佳方法是通过进度对话框。
安卓正是围绕这一点构建的,因为它可以帮助用户了解幕后发生的事情

您可以使用:

    progress.setCanceledOnTouchOutside(false);
    progress.setCancelable(false);

在加载时阻止进一步的用户操作。

您的问题太简单了-到目前为止,我不确定处理程序是否正在生成延迟,或者您希望使用处理程序阻止用户操作。还请提供您尝试的详细信息和理想的代码片段。我需要3秒的延迟来完成按钮上的操作。所以,若用户点击任何其他按钮,它会使一些错误。所以我想阻止用户在3秒钟内做任何事情。我不能在我的应用程序中使用
对话框
,因为我需要3秒钟的延迟才能在
按钮上执行操作。所以,如果用户单击任何其他
按钮
。这会让事情变得不对劲。所以我想阻止用户在3秒内做任何事情。
    progress.setCanceledOnTouchOutside(false);
    progress.setCancelable(false);