Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 如何在几秒钟不活动后关闭对话框?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@L_197_0@一个包含对话框的应用程序

我希望在x秒之后关闭此对话框,此时用户没有与应用程序进行任何交互,例如音量搜索栏弹出窗口(单击音量按钮时打开,并且在2秒钟不活动后关闭).
实现这个的最简单方法是什么?

谢谢

解决方法

例如,每次用户与对话框交互时,您都可以使用Handler并调用其.removeCallBACks()和.postDelayed()方法.

在进行交互时,.removeCallBACks()方法将取消.postDelayed()的执行,之后,你将使用.postDelayed()启动一个新的Runnable

在此Runnable中,您可以关闭对话框.

// a dialog
    final Dialog dialog = new Dialog(getApplicationContext());

    // the code inside run() will be executed if .postDelayed() reaches its delay time
    final Runnable runnable = new Runnable() {

        @Override
        public void run() {
            dialog.dismiss(); // hide dialog
        }
    };

    Button interaction = (Button) findViewById(R.id.bottom);

    final Handler h = new Handler();

            // pressing the button is an "interaction" for example
    interaction.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {


            h.removeCallBACks(runnablE); // cancel the running action (the hiding process)
            h.postDelayed(runnable,5000); // start a new hiding process that will trigger after 5 seconds
        }
    });

要跟踪用户互动,您可以使用:

@Override
public void onUserInteraction(){
    h.removeCallBACks(runnablE); // cancel the running action (the hiding process)
    h.postDelayed(runnable,5000); // start a new hiding process that will trigger after 5 seconds
}

您的活动中有哪些内容.

大佬总结

以上是大佬教程为你收集整理的android – 如何在几秒钟不活动后关闭对话框?全部内容,希望文章能够帮你解决android – 如何在几秒钟不活动后关闭对话框?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。