OnTouchListener 工作不正常

发布于 2025-01-05 11:50:21 字数 2133 浏览 0 评论 0原文

我在使用 OnTouchListener 时遇到一些问题。我希望每次当用户触摸屏幕进入某个活动时。问题是,当我触摸屏幕时,它正在对当前活动进行刷新之类的操作并启动(当前活动包含一个 ViewFlipper,当触摸屏幕时,viewflipper 开始从头开始翻转) )。仅在第二次触摸时,应用程序才会进入下一个活动。谁能帮我解决这个问题吗?

我当前的活动扩展了 OnTouchListener ,我设置了 myviewflipper.setonTouchListener(this) ,我的 onTouch() 方法是:

@Override
    public boolean onTouch(View v, MotionEvent event) {
            finish=true;
        Intent intent = new Intent(getBaseContext(), FinishApplication.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        finish();
        return true;
    }

触摸屏幕后调用onPause()onResume()。在 onResume() 方法中,我启动了一些计时器来进行一些更新(当进行更新时,幻灯片开始),我认为这就是我的活动刷新的原因。仅在触摸屏幕后,我将变量布尔 finish 设置为 true,并且仅当 finishfalse 时才开始更新> 但不工作。我的变量未设置为 true。还有其他想法如何解决这个问题吗?

@Override
    protected void onResume() {
        super.onResume();
        System.out.println("OnResume ViewPlaylist!!");
        t = new Timer();
        t.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    public void run() {
                        Imalive();
                    }
                });

            }

        }, 300, 30000);

        if (finish != true) {
            System.out.println("Start update!");
            update = new Timer();
            update.scheduleAtFixedRate(new TimerTask() {

                @Override
                public void run() {
                    runOnUiThread(new Runnable() {
                        public void run() {
                            updateItems();
                        }
                    });
                }

            }, 30000, 20000);
        } else {
            finish = true;
            Intent intent = new Intent(SlideShow.this, FinishApplication.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            finish();


}
}

I have some problems with OnTouchListener. I want that everytime when user touch the screen to go to a certain activity. The problem is that when I touch the screen it is making something like an refresh for the present activity and it starts (the current activity contains a ViewFlipper and when the screen is touched the viewflipper starts flipping from the begining). Only at the second touch the app goes to next activity. Can anyone help me to solve this?

My current activity extends OnTouchListener , I set myviewflipper.setonTouchListener(this) and my onTouch() method is :

@Override
    public boolean onTouch(View v, MotionEvent event) {
            finish=true;
        Intent intent = new Intent(getBaseContext(), FinishApplication.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        finish();
        return true;
    }

After I touch the screen is called onPause() and onResume(). On onResume() method I start some timers for some updates (when updates are made, the slideshow starts) and I think that's the reason why my activity is refresh. I set a variable boolean finish to true only after the screen is touch and start the updates only if finish is false but is not working. My variable is not set to true. Any other idea how can I solve this ?

@Override
    protected void onResume() {
        super.onResume();
        System.out.println("OnResume ViewPlaylist!!");
        t = new Timer();
        t.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    public void run() {
                        Imalive();
                    }
                });

            }

        }, 300, 30000);

        if (finish != true) {
            System.out.println("Start update!");
            update = new Timer();
            update.scheduleAtFixedRate(new TimerTask() {

                @Override
                public void run() {
                    runOnUiThread(new Runnable() {
                        public void run() {
                            updateItems();
                        }
                    });
                }

            }, 30000, 20000);
        } else {
            finish = true;
            Intent intent = new Intent(SlideShow.this, FinishApplication.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            finish();


}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

两人的回忆 2025-01-12 11:50:21

我通过在 onPause() 方法上调用 finish() 来解决这个问题。

I solve this by calling finish() on onPause() method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文