我怎样才能阻止用户在使用处理程序的延迟期间做任何事情?

How can I prevent the user from doing anything during a delay using a Handler?

我知道这个问题可能不够清楚。有没有一种方法可以防止用户在延迟(3 秒)期间使用 Handler .

在屏幕上单击或执行任何操作

我认为你最好用 setCancelable(false) 显示对话框(等待对话框...) 3 秒。

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

要阻止的代码:

getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);

解锁(清除标记):

getWindow().clearFlags(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 文件 loading.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>

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

您可以使用:

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

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