长按加号减号快速加减
//减号长按的话就快速加减
tv_jianhao.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
handler.sendEmptyMessageDelayed(0, 500);
break;
case MotionEvent.ACTION_UP:
handler.removeCallbacksAndMessages(null);
break;
}
return false;
}
});
//加号长按的话就快速加减
tv_jiahao.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
handler.sendEmptyMessageDelayed(1, 500);
break;
case MotionEvent.ACTION_UP:
handler.removeCallbacksAndMessages(null);
break;
}
return false;
}
});
创建Handler去控制加减
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 0) {
try {
String countString = tv_count.getText().toString().trim();
if (countString.equals("0")) {
return;
}
int count = Integer.parseInt(countString) + One;
tv_count.setText("" + count);
sendEmptyMessageDelayed(1, 100);
} catch (Exception e) {
e.printStackTrace();
}
} else if (msg.what == 1) {
try {
String countString = tv_count.getText().toString().trim();
int count = Integer.parseInt(countString) + One;
tv_count.setText("" + count);
sendEmptyMessageDelayed(1, 100);
} catch (Exception e) {
e.printStackTrace();
}
}
}
};