Android簡單的handler處理

來源:互聯網
上載者:User

標籤:android   style   blog   color   os   io   for   art   

 1 package com.example.androidprojecttest; 2  3 import android.os.Bundle; 4 import android.os.Handler; 5 import android.os.Message; 6 import android.os.Messenger; 7 import android.R.id; 8 import android.app.Activity; 9 import android.content.Intent;10 import android.view.Menu;11 import android.view.View;12 import android.view.View.OnClickListener;13 import android.widget.Button;14 import android.widget.LinearLayout;15 import android.widget.TextView;16 17 public class MainActivity extends Activity {18 19     private TextView tView;20 21     private Handler mHandler = new Handler() {22         @Override23         public void handleMessage(Message msg) {24             // TODO 接收訊息並且去更新UI線程上的控制項內容25             if (msg.what == 1) {26                 tView.setText(String.valueOf(msg.obj));27             }28             super.handleMessage(msg);29         }30 31     };32 33     @Override34     protected void onCreate(Bundle savedInstanceState) {35         super.onCreate(savedInstanceState);36         setContentView(R.layout.activity_main);37         tView = (TextView) findViewById(R.id.tv);38 39         new Thread() {40             public void run() {41                 try {42                     for (int i = 0; i < 100; i++) {43                         Thread.sleep(500);44                         Message msgMessage=new Message();45                         msgMessage.what=1;46                         msgMessage.obj="更新後的值是:"+i;47                         mHandler.sendMessage(msgMessage);48                     }49                 } catch (Exception e) {50                     e.printStackTrace();51                 }52             }53         }.start();54     }55 56 }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.