• Android Handler简单使用


     1 package com.example.myhandlertest3;
     2 
     3 import android.os.Bundle;
     4 import android.os.Handler;
     5 import android.os.Message;
     6 import android.support.v7.app.ActionBarActivity;
     7 import android.view.View;
     8 import android.view.View.OnClickListener;
     9 import android.widget.Button;
    10 import android.widget.TextView;
    11 /**
    12  * Android Handler
    13  * @author shaobn
    14  * @date 2015/9/9
    15  */
    16 public class MainActivity extends ActionBarActivity {
    17     private Button button;
    18     private TextView tv;
    19     private Handler handler; 
    20     @Override
    21     protected void onCreate(Bundle savedInstanceState) {
    22         super.onCreate(savedInstanceState);
    23         setContentView(R.layout.activity_main);
    24         button = (Button)this.findViewById(R.id.button1);
    25         handler = new Handler(){
    26             
    27             public void handleMessage(android.os.Message msg) {
    28                 
    29                 Bundle bundle = msg.getData();
    30                 String data = bundle.getString("color");
    31                 MainActivity.this.button.append(data);
    32             };
    33         };
    34         final Runnable runnable = new Runnable() {
    35             
    36             @Override
    37             public void run() {
    38                 // TODO Auto-generated method stub
    39                 String color = "red";
    40                 Message message = new Message();
    41                 Bundle bundle = new Bundle();
    42                 bundle.putCharSequence("color", color);
    43                 message.setData(bundle);
    44                 MainActivity.this.handler.sendMessage(message);
    45                 try {
    46                     Thread.sleep(3000);
    47                 } catch (Exception e) {
    48                     // TODO: handle exception
    49                     e.printStackTrace();
    50                 }
    51             }
    52         };
    53         button.setOnClickListener(new OnClickListener() {
    54             
    55             @Override
    56             public void onClick(View arg0) {
    57                 // TODO Auto-generated method stub
    58                 new Thread(runnable).start();
    59             }
    60         });
    61     }
    62 
    63     
    64 }

    自己随便写的一个demo,记录一下。

    吾宁做一叶扁舟,始航于湖边,遨游于海上,浪迹于江中。
  • 相关阅读:
    关于supervisor无法监控golang代码的解决方法
    [2017BUAA软工]提问回顾
    [2017BUAA软工]个人阅读作业+总结
    解决nginx+uWSGI部署Django时遇到的static文件404的问题
    [2017BUAA软工]个人项目心得体会:数独
    Week2 Programming Assignment: Linear Regression
    eclipse如何同步自己的preference(oomph preference recorder)
    javac
    Apache Maven Compiler Plugin
    数据库与数据仓库的区别
  • 原文地址:https://www.cnblogs.com/assassin666/p/4794720.html
Copyright © 2020-2023  润新知