(尚学堂)消息机制与异步任务——自动增加数值

(尚学堂)消息机制与异步任务——自动增加数值,第1张

(尚学堂)消息机制与异步任务——自动增加数值

 

package com.example.testapplication;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class HandlerDemoActivity extends ApplicationActivity implements View.onClickListener {
    private static final int WHAT_INCREASE = 1;
    private static final int WHAT_DECREASE = 2;
    private TextView tv_demo_number;
    private Button btn_demo_increase;
    private Button btn_demo_decrease;
    private Button btn_demo_pause;

    // 创建handler线程
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            // 得到当前显示的数值
            int number = Integer.parseInt(tv_demo_number.getText().toString());
            switch (msg.what){
                case WHAT_INCREASE:
                    // 限制number <= 20
                    if(number == 20){
                        // 设置暂停不能操作
                        btn_demo_pause.setEnabled(false);
                        Toast.makeText(HandlerDemoActivity.this, "已经达到最大值", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    number++;
                    tv_demo_number.setText(number +"");
                    // 发送增加的延迟消息
                    handler.sendEmptyMessageDelayed(WHAT_INCREASE,1000);
                    break;
                case WHAT_DECREASE:
                    // 限制number>=1
                    if(number ==1){
                        // 设置暂停不能 *** 作
                        btn_demo_pause.setEnabled(false);
                        Toast.makeText(HandlerDemoActivity.this, "已经是最小值", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    number--;
                    tv_demo_number.setText(number+"");
                    // 发送减少的延迟消息
                    handler.sendEmptyMessageDelayed(WHAT_DECREASE,1000);
                    break;
                default:
                    break;
            }
            
        }
    };
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_handler6);
        init();
    }

    private void init() {
        tv_demo_number = (TextView) findViewById(R.id.tv_demo_number);
        btn_demo_increase = (Button) findViewById(R.id.btn_demo_increase);
        btn_demo_decrease = (Button) findViewById(R.id.btn_demo_decrease);
        btn_demo_pause = (Button) findViewById(R.id.btn_demo_pause);

        btn_demo_increase.setonClickListener(this);
        btn_demo_decrease.setonClickListener(this);
        btn_demo_pause.setonClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if(view== btn_demo_increase){ //自动增加  what =1
            // 限制Button可 *** 作性
            btn_demo_increase.setEnabled(false);
            btn_demo_decrease.setEnabled(true);
            btn_demo_pause.setEnabled(true);
            // 停止减少(移除未处理的减少的消息)
            handler.removeMessages(WHAT_DECREASE);
            // 发消息(增加)
            handler.sendEmptyMessage(WHAT_INCREASE);
        }else if(view==btn_demo_decrease) {//自动减少 what=2
            //限制Button可 *** 作性
            btn_demo_increase.setEnabled(true);
            btn_demo_decrease.setEnabled(false);
            btn_demo_pause.setEnabled(true);

            //停止增加(移除未处理的增加的消息)
            handler.removeMessages(WHAT_INCREASE);

            //发消息(减少)
            handler.sendEmptyMessage(WHAT_DECREASE);
        }else if(view==btn_demo_pause) {//暂停

            //限制Button可 *** 作性
            btn_demo_increase.setEnabled(true);
            btn_demo_decrease.setEnabled(true);
            btn_demo_pause.setEnabled(false);

            //停止增加/减少(移除未处理的减少/增加的消息)
            handler.removeMessages(WHAT_INCREASE);
            handler.removeMessages(WHAT_DECREASE);
        }
    }
}


    

    

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5609073.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存