一个简易的数字加减器,可以用来选择物品数量,如图:
效果:
代码
public class SwitchNumber extends LinearLayout{
private Context context;
private EditText switch_number_show;
private final int maximum = 999;
private int number=1;
private boolean isAdding=true;
private long startTime;
private int threadSleepTime;
private long timePassed;
@SuppressLint("ClickableViewAccessibility")
public SwitchNumber(Context context) {
super(context);
this.context = context;
LayoutInflater.from(context).inflate(R.layout.view_switch_number, this, true);
Button switch_number_reduce = findViewById(R.id.switch_number_reduce);
switch_number_show = findViewById(R.id.switch_number_show);
Button switch_number_add = findViewById(R.id.switch_number_add);
//默认显示 1
int showNumber = 1;
switch_number_show.setText(String.valueOf(showNumber));
//点击加号
switch_number_add.setOnTouchListener((view, motionEvent) -> {
if (motionEvent.getAction()==MotionEvent.ACTION_DOWN){
startTime = System.currentTimeMillis();
isAdding = true;
changeNumber(0);
}
else if (motionEvent.getAction()==MotionEvent.ACTION_UP){
isAdding = false;
}
return true;
});
//点击减号
switch_number_reduce.setOnTouchListener((view, motionEvent) -> {
if (motionEvent.getAction()==MotionEvent.ACTION_DOWN){
startTime = System.currentTimeMillis();
isAdding = true;
changeNumber(1);
}
else if (motionEvent.getAction()==MotionEvent.ACTION_UP){
isAdding = false;
}
return true;
});
//内容监听
switch_number_show.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
if (TextUtils.isEmpty(editable.toString())){
switch_number_show.setText(String.valueOf(showNumber));
}
}
});
}
private void changeNumber(int flag){
//线程执行
ThreadPoolManager.getInstance().execute(new Runnable() {
@Override
public void run() {
number = Integer.parseInt(switch_number_show.getText().toString());
while (isAdding) {
if (flag==1){
if (number>1)number--;
else {
isAdding=false;
Looper.prepare();
Toast.makeText(context, "至少选择1", Toast.LENGTH_SHORT).show();
Looper.loop();
break;
}
}
else{
if (number<maximum)number++;
else {
isAdding=false;
Looper.prepare();
Toast.makeText(context, "最多选择"+maximum, Toast.LENGTH_SHORT).show();
Looper.loop();
break;
}
}
timePassed = System.currentTimeMillis() - startTime;
switch_number_show.post(() -> switch_number_show.setText(String.valueOf(number)));
//时间越来越快
if (timePassed > 7000){
threadSleepTime = 20;
}
else if (timePassed > 2000){
threadSleepTime = 50;
} else {
threadSleepTime = 100;
}
try {
Thread.sleep(threadSleepTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
}
//获取数量
public int getNumber(){
return Integer.parseInt(switch_number_show.getText().toString());
}
}
线程管理
public final class ThreadPoolManager extends ThreadPoolExecutor {
private static volatile ThreadPoolManager sInstance;
private ThreadPoolManager() {
super(0, 200, 30L, TimeUnit.MILLISECONDS, new SynchronousQueue<>());
}
public static ThreadPoolManager getInstance() {
if(sInstance == null) {
synchronized (ThreadPoolManager.class) {
if(sInstance == null) {
sInstance = new ThreadPoolManager();
}
}
}
return sInstance;
}
}
资源文件
round_corner.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!--圆角半径-->
<!-- rounded corner -->
<corners android:radius="10dp" />
<!--填充颜色-->
<solid android:color="#E4E4E4" />
</shape>