0

我正在寻找一种在按下按钮并按住一秒钟时调用函数的方法。当按钮被释放时,另一个函数应该被调用。

我正在考虑一个 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
    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

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

4

2 回答 2

0

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

于 2013-11-14T15:09:57.603 回答
0

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

于 2013-11-14T15:45:00.407 回答