首页 > 其他 > 详细

按钮长按,值按频率变化。

时间:2014-04-19 06:42:55      阅读:610      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
package com.coretech.defobject;
import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Button;

public class LongPressButton extends Button {
    private int mLastMotionX, mLastMotionY;
    // 是否移动了
    private boolean isMoved;
    // 是否释放了
    private boolean isReleased;
    // 计数器,防止多次点击导致最后一次形成longpress的时间变短
    private int mCounter;
    // 长按的runnable
    private Runnable mLongPressRunnable;
    // 移动的阈值
    private static final int TOUCH_SLOP = 20;
    //激活的频率
    private static final int PRESS_INTERVAL = 200;
    
    private final Handler mHandler = new Handler();

    public LongPressButton(Context context) {
        super(context);

    }

    public LongPressButton(Context context, AttributeSet attr) {   
        //必须重写,否则android.view.InflateException: Binary XML file line #165: Error inflating class
        super(context, attr);

    }
    private void createRunnable() {
        mLongPressRunnable = new Runnable() {

            @Override
            public void run() {
                mCounter--;
                // 计数器大于0,说明当前执行的Runnable不是最后一次down产生的。
                if (mCounter > 0 || isReleased || isMoved)
                    return;
                performLongClick();
                mHandler.postDelayed(this, PRESS_INTERVAL);
            }
        };
        mHandler.postDelayed(mLongPressRunnable, 10);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            mLastMotionX = x;
            mLastMotionY = y;
            mCounter++;
            isReleased = false;
            isMoved = false;
            // int iTime = ViewConfiguration.getLongPressTimeout();
            createRunnable();
            break;
        case MotionEvent.ACTION_MOVE:
            if (isMoved)
                break;
            if (Math.abs(mLastMotionX - x) > TOUCH_SLOP
                    || Math.abs(mLastMotionY - y) > TOUCH_SLOP) {
                // 移动超过阈值,则表示移动了
                isMoved = true;
            }
            break;
        case MotionEvent.ACTION_UP:
            // 释放了
            isReleased = true;
            break;
        }
        return true;
    }
}
bubuko.com,布布扣

引用

bubuko.com,布布扣
btn_weight_dec.setOnLongClickListener(new OnLongClickListener() {        
            @Override
            public boolean onLongClick(View v) {
                tv_weight.setText("" + (Float.parseFloat(tv_weight.getText().toString()) - 0.1));
                return false;
            }
        });
bubuko.com,布布扣

 

按钮长按,值按频率变化。,布布扣,bubuko.com

按钮长按,值按频率变化。

原文:http://www.cnblogs.com/profession/p/3673204.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!