Android Handler簡單使用

來源:互聯網
上載者:User

標籤:

 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 Handler13  * @author shaobn14  * @date 2015/9/915  */16 public class MainActivity extends ActionBarActivity {17     private Button button;18     private TextView tv;19     private Handler handler; 20     @Override21     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             @Override37             public void run() {38                 // TODO Auto-generated method stub39                 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 exception49                     e.printStackTrace();50                 }51             }52         };53         button.setOnClickListener(new OnClickListener() {54             55             @Override56             public void onClick(View arg0) {57                 // TODO Auto-generated method stub58                 new Thread(runnable).start();59             }60         });61     }62 63     64 }

自己隨便寫的一個demo,記錄一下。

Android Handler簡單使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.