安卓长按事件

12

我有两个按钮,它们可以通过单击进行递增和递减操作,并且在onClickListner下运行良好。我发现存在onLongClickListener,我认为它是用于长按事件的。如果按住按钮,如何使数字快速递增/递减?

我是否正确地认为onLongClickListener仅在长时间点击时触发一次?是否有更合适的侦听器或我不知道的某些属性?


尝试使用http://developer.android.com/reference/android/view/View.OnTouchListener.html。在ontouch中,它会给你一个motionevent。您可以检查动作向下和动作向上。您实际上是在自己实现ontouch和onlongtouch。 - Falmarri
我想要的是在按钮按下时每200毫秒更新一次x值和TextView的文本。很抱歉我对Java不是很熟悉,我的大部分经验都是在ASP/PHP类型的交互性不太强的东西上。如果有人能提供示例代码或指导正确的方法,我将不胜感激,因为我甚至不知道从哪里开始。 - David Perry
使用Handler.sendDelayedMessage()方法,每隔x毫秒向自己发送一条消息,直到收到up事件。 - hackbod
2个回答

17

你可以按照以下代码来实现。

package org.me.rapidchange;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.TextView;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class MainActivity extends Activity implements OnKeyListener, 
        OnTouchListener, OnClickListener {
    private class UpdateCounterTask implements Runnable {
        private boolean mInc;

        public UpdateCounterTask(boolean inc) {
            mInc = inc;
        }

        public void run() {
            if (mInc) {
                mHandler.sendEmptyMessage(MSG_INC);
            } else {
                mHandler.sendEmptyMessage(MSG_DEC);
            }
        }
    }

    private static final int MSG_INC = 0;
    private static final int MSG_DEC = 1;

    private Button mIncButton;
    private Button mDecButton;
    private TextView mText;
    private int mCounter;

    private Handler mHandler;
    private ScheduledExecutorService mUpdater;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case MSG_INC:
                        inc();
                        return;
                    case MSG_DEC:
                        dec();
                        return;
                }
                super.handleMessage(msg);
            }
        };
        mIncButton = (Button) findViewById(R.id.inc_button);
        mDecButton = (Button) findViewById(R.id.dec_button);
        mText = (TextView) findViewById(R.id.text);
        mIncButton.setOnTouchListener(this);
        mIncButton.setOnKeyListener(this);
        mIncButton.setOnClickListener(this);
        mDecButton.setOnTouchListener(this);
        mDecButton.setOnKeyListener(this);
        mDecButton.setOnClickListener(this);
    }

    private void inc() {
        mCounter++;
        mText.setText(Integer.toString(mCounter));
    }

    private void dec() {
        mCounter--;
        mText.setText(Integer.toString(mCounter));
    }

    private void startUpdating(boolean inc) {
        if (mUpdater != null) {
            Log.e(getClass().getSimpleName(), "Another executor is still active");
            return;
        }
        mUpdater = Executors.newSingleThreadScheduledExecutor();
        mUpdater.scheduleAtFixedRate(new UpdateCounterTask(inc), 200, 200,
                TimeUnit.MILLISECONDS);
    }

    private void stopUpdating() {
        mUpdater.shutdownNow();
        mUpdater = null;
    }

    public void onClick(View v) {
        if (mUpdater == null) {
            if (v == mIncButton) {
                inc();
            } else {
                dec();
            }
        }
    }

    public boolean onKey(View v, int keyCode, KeyEvent event) {
        boolean isKeyOfInterest = keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER;
        boolean isReleased = event.getAction() == KeyEvent.ACTION_UP;
        boolean isPressed = event.getAction() == KeyEvent.ACTION_DOWN
                && event.getAction() != KeyEvent.ACTION_MULTIPLE;

        if (isKeyOfInterest && isReleased) {
            stopUpdating();
        } else if (isKeyOfInterest && isPressed) {
            startUpdating(v == mIncButton);
        }
        return false;
    }

    public boolean onTouch(View v, MotionEvent event) {
        boolean isReleased = event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL;
        boolean isPressed = event.getAction() == MotionEvent.ACTION_DOWN;

        if (isReleased) {
            stopUpdating();
        } else if (isPressed) {
            startUpdating(v == mIncButton);
        }
        return false;
    }
}

这个工作得非常好,我甚至觉得我理解了它是如何工作的,哈哈。谢谢! - David Perry

7

我有同样的目标,并最终使用OnLongClick来捕获按下部分以通过处理程序启动重复事件,然后使用正常的OnClick来捕获释放并停止它。对我来说效果非常好。

mOngoingRunnable = new Runnable() {
    public void run() {
        // do stuff
        mHandler.postDelayed(mOngoingRunnable, delayMsecs);
    }
};

public boolean onLongClick(View view) {
    mHandler.post(mOngoingRunnable);
    mOngoing = true;
    return false;
}

public void onClick(View view) {
    if (mOngoing) {
        mHandler.removeCallbacks(mOngoingRunnable);
        mOngoing = false;
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接