首页 > 解决方案 > 即使用户不再触摸按钮,是否可以延长 android 上的按钮触摸效果?

问题描述

我需要能够让应用程序运行起来,就好像按钮触摸的持续时间比现实世界中实际的时间长。

例如,用户触摸一个按钮 1 秒,应用程序在该触摸上添加了 X 量的预定义时间,设备认为用户在 X 量时间过去后才停止按下按钮,即使用户已经移动他们的手指在 X 时间设置之前设置,这恰好在用户停止触摸按钮的那一刻设置。

标签: javaandroidbuttonontouch

解决方案


在您的视图中添加触摸侦听器并在 onTouch() 方法中实现您的逻辑。像这样:

 button.setOnTouchListener(new View.OnTouchListener() {

                    @Override
                    public boolean onTouch(View view, MotionEvent event) {
                        if (event.getEventTime() > Expected time) {

                            // implement your logic here
                            AlertDialog alertDialog = new 
                            AlertDialog.Builder(MainActivity.this).create();
                            alertDialog.setTitle("Alert");
                            alertDialog.setMessage("Alert message to be shown");
                            alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int which) {
                                            dialog.dismiss();
                                        }
                                    });
                            alertDialog.show();
                        button.setEnabled(false);


                            return true;

                        }
                        return true;

                    }

                });

推荐阅读