简体   繁体   English

具有OnTouchListener的按钮,可在Android Studio中持续执行某些操作

[英]Button with OnTouchListener that continuously does something while held down in Android Studio

I am a noob to Android Studio, but am creating an application for a class for school. 我是Android Studio的菜鸟,但我正在创建一个学校课程的应用程序。 It is a max bench press calculator. 这是一个最大的卧推计算器。 I would like to make it so that when the increase or decrease weight button is held down, then the weight will continuously go up or down. 我想这样做,以便当按下增加或减少重量按钮时,重量将持续上升或下降。 I saw a couple of questions on this, but they were very confusing and weren't very well explained. 我在这看到了几个问题,但是它们非常令人困惑,并且没有得到很好的解释。 I would rather not put code in that I don't know how it works, just to make the application run. 我宁愿不把代码放在我不知道它是如何工作的只是让应用程序运行。 Thanks!! 谢谢!!

You can achieve such an effect by creating the button then setting it's onTouchListener to call certain methods when certain types of touch events are detected on the button. 您可以通过创建按钮然后将其设置为onTouchListener以在按钮上检测到某些类型的触摸事件时调用某些方法来实现此类效果。 For example: 例如:

increaseWeightButton.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent mEvent) {
        if (mEvent.getAction() == MotionEvent.ACTION_DOWN)
            startIncreasingWeight();
        else if (mEvent.getAction() == MotionEvent.ACTION_UP)
            stopIncreasingWeight();
        return false;
    }
});

edit: Can you try putting the button.setOnTouchListener code just after creating the button in the onCreate() rather than outside? 编辑:你可以尝试在onCreate()而不是在外面创建按钮后放置button.setOnTouchListener代码吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM