zoukankan      html  css  js  c++  java
  • Handler(消息机制)

    Demo演示

    //通过Handler事件倒计时的一个操作,并判断状态

    public class MainActivity extends AppCompatActivity {
    private TextView mTextView;
    private Button mButton,mButton2;
    private final static int STATE_START = 100;
    private final static int STATE_RUN = 101;
    private final static int STATE_END = 102;
    //创建Handler对象
    private Handler mHandler = new Handler() {
    public void handleMessage(Message msg){
    switch (msg.what){
    case STATE_START:
    mTextView.setText("倒计时开始");
    break;
    case STATE_RUN:
    mTextView.setText(String.valueOf(msg.arg1));
    break;
    case STATE_END:
    mTextView.setText("倒计时结束");
    break;
    }
    }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mTextView = (TextView)findViewById(R.id.textview);
    mButton = (Button)findViewById(R.id.button);
    mButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    MyThread myThread = new MyThread(10);
    myThread.start();
    }
    });
    }
    class MyThread extends Thread{
    int startnum;
    MyThread(int num){
    startnum = num ;

    }

    @Override
    public void run() {
    super.run();
    Message startMsg = mHandler.obtainMessage();
    startMsg.what=STATE_START;
    mHandler.sendMessage(startMsg);
    while (startnum >=0){
    try {
    Thread.sleep(1000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    Message runMsg = mHandler.obtainMessage();
    runMsg.what =STATE_RUN;
    runMsg.arg1 = startnum;
    mHandler.sendMessage(runMsg);
    startnum--;

    }
    Message endMsg = mHandler.obtainMessage();
    endMsg.what = STATE_END;
    mHandler.sendMessage(endMsg);
    }
    }
    }


    //演示post() postAtTime() postdelayed()
    public class NextActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView mTextView;
    private Button button_post, button_postattime, button_postdelayed;
    private Handler mHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_next);
    mTextView = (TextView) findViewById(R.id.textview_next);
    button_post = (Button) findViewById(R.id.button_next_post);
    button_postattime = (Button) findViewById(R.id.button_next_postattime);
    button_postdelayed = (Button) findViewById(R.id.button_next_postdelayed);
    button_postdelayed.setOnClickListener(this);
    button_post.setOnClickListener(this);
    button_postattime.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {
    switch (v.getId()) {
    case R.id.button_next_post:
    mHandler.post(new Runnable() {
    @Override
    public void run() {
    mTextView.setText("立即执行");
    }
    });
    break;
    case R.id.button_next_postattime:
    mHandler.postAtTime(new Runnable() {
    @Override
    public void run() {
    mTextView.setText("指定时间执行");
    }
    },android.os.SystemClock.uptimeMillis() +2000); //PostAtTime的时间使用的是uptimeMillis()不是currentTimeMillis()
    break;
    case R.id.button_next_postdelayed:
    mHandler.postDelayed(new Runnable() {
    @Override
    public void run() {
    mTextView.setText("延迟时间执行");
    }
    }, 2000);
    break;
    }
    }
    }

  • 相关阅读:
    课后作业
    使用类的静态字段和构造函数,我们可以跟踪某个类所创建对象的个数。请写一个类,在任何时候都可以向它查询“你已经创建了多少个对象?”。
    课程作业·02
    课程作业01
    课程作业02 将课程中的所有动手动脑的问题以及课后实验性的问题,整理成一篇文档。
    课程作业01 模仿JavaAppArguments.java示例,编写一个程序,此程序从命令行接收多个数字,求和之后输出结果。
    《大道至简》第一章伪代码
    Vue2.0版英雄联盟助手,我的第一个小开源项目
    二级下拉菜单的三种实现方法——CSS 、JS、 jQuery
    关于清除浮动 and position的一些注意点
  • 原文地址:https://www.cnblogs.com/niupi/p/5512249.html
Copyright © 2011-2022 走看看