繁体   English   中英

如何检查秒后是否仍单击按钮

[英]How to check if Button is still clicked after second

我正在寻找一种按住按钮一秒钟的方法来调用函数。 释放按钮时,应调用另一个功能。

我当时在考虑onLongClickedListener,但这对我来说不太好,因为要显示的文本会太长或太短。 我认为TouchListener可以为我提供帮助,因为Action_Up事件可以让我选择不再按下按钮时使文本消失。 当按下按钮时,Action_down事件给了我,我认为可以在按下按钮时启动计时器,等待一秒钟,再次检查按钮是否仍被按下,然后调用该函数(显示文本)。

@Override
public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub

    switch (v.getId()) {
    // the button. I set bFertig.setOnTouchListener(this); in onCreate
    case R.id.bFertig:
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            // everything works up to here
            waitTimerNotif = new CountDownTimer(1000, 500) {
                @Override
                public void onTick(long arg0) {}

                @Override
                public void onFinish() {
                    // TODO Auto-generated method stub
                    // here im checking if the button still is pressed
                    if (bFertig.isPressed()) {
                        // It never goes into here
                        ShowNotifBox("Fertig", "fertig", false, false,false);
                    }
                }
            }.start();
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            DissapearNotifBox();
            Log.d("Debug", "Button released");
        }
        break;
    }
    return true;
}

对于xml中的Button:

<Button
    android:id="@+id/bFertig"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:background="@drawable/fertigbutton"
    android:gravity="center"
    android:textColor="#ffffff"
    android:textSize="18.5dp" 
    android:clickable="true"/> <!--Googleing suggested I need this for isPressed() to work but it didnt help

任何想法,我做错了什么或为什么这不起作用? 谢谢!

为什么不使用setOnLongClickListener函数? 这应该可以解决您的问题。 可以在此处找到一个示例: setOnLongClickListener

您返回的是true,这表示系统正在处理触摸事件,这意味着按钮没有,这意味着它可能没有更新其按下状态。 尝试返回false。

暂无
暂无

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

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