0

I am looking for a way to call a function when a button is pressed and held for one second. When the button is released another function should be called.

I was thinking about an onLongClickedListener but this won't work well for me since the text that is going to be displayed would stay too long or short. I am thinking a TouchListener could help me because the Action_Up event would give me the option to let the text dissapear when the button isn't pressed anymore. The Action_down event gives me when the button is pressed and I thought I could start a timer when the button is pressed, wait a second, check again if the button still is pressed and then call the function (show the text).

@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;
}

For the Button in 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

Any ideas what I did wrong or why this isnt working? Thanks!

4

2 に答える 2

0

true を返しています。これは、タッチ イベントを処理していることをシステムに伝えます。これは、ボタンが処理されていないことを意味します。つまり、押された状態が更新されていない可能性があります。false を返してみてください。

于 2013-11-14T15:45:00.407 に答える
0

なぜsetOnLongClickListener機能を使用しないのですか?これで問題が解決するはずです。例はここにあります: setOnLongClickListener

于 2013-11-14T15:09:57.603 に答える