【问题标题】:Is it possible to prolong a button touch effect on android, even though the user no longer touches the button?即使用户不再触摸按钮,是否可以延长 android 上的按钮触摸效果?
【发布时间】:2019-05-22 00:08:12
【问题描述】:

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

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

【问题讨论】:

  • 没有简单的方法可以做到这一点。您需要扩展 View 并覆盖其触摸事件。

标签: java android button ontouch


【解决方案1】:

在您的视图中添加触摸侦听器并在 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;

                    }

                });

【讨论】:

    猜你喜欢
    • 2016-10-13
    • 1970-01-01
    • 2015-05-13
    • 1970-01-01
    • 1970-01-01
    • 2013-07-11
    • 1970-01-01
    • 2012-06-20
    • 2023-03-19
    相关资源
    最近更新 更多