学习在新线程中使用Handler更新UI的方法
2013-10-02 13:38
417 查看
学习在新线程中使用Handler更新UI的方法: package com.example.hnadlerapp; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { private TextView text; private Button button; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); text = (TextView)findViewById(R.id.text); button = (Button)findViewById(R.id.button); text.setText("通过线程连接..."); button.setOnClickListener(new ButtonListener()); } Handler handler = new Handler() { public void handleMessage(Message msg) { String s; s = (String)msg.obj; text.setText(s); } }; class ButtonListener implements OnClickListener { @Override public void onClick(View arg0) { Mythread thread = new Mythread(); thread.start(); } } class Mythread extends Thread { public void run() { try { Thread.sleep(2000); } catch(Exception e) { e.printStackTrace(); } String s = "返回成功"; //从handler的obtainMessage方法取得消息对象 Message msg = handler.obtainMessage(); //把字符串给消息对象的object msg.obj = s; //调用handler的sendMessage方法把消息对象传入后,系统自动调用handler 的handleMessage方法 handler.sendMessage(msg); } } } 全部代码: package com.example.hnadlerapp; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { private TextView text; private Button button; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); text = (TextView)findViewById(R.id.text); button = (Button)findViewById(R.id.button); text.setText("通过线程连接..."); button.setOnClickListener(new ButtonListener()); } Handler handler = new Handler() { public void handleMessage(Message msg) { String s; s = (String)msg.obj; text.setText(s); } }; class ButtonListener implements OnClickListener { @Override public void onClick(View arg0) { Mythread thread = new Mythread(); thread.start(); } } class Mythread extends Thread { public void run() { try { Thread.sleep(2000); } catch(Exception e) { e.printStackTrace(); } String s = "返回成功"; Message msg = handler.obtainMessage(); msg.obj = s; handler.sendMessage(msg); } } }
相关文章推荐
- 横屏与竖屏shouldAutorotateToInterfaceOrientation
- IOS之UILabel显示内容自动换行
- C# window Service实现调用有UI的应用程序(关于win xp以后的window系统)
- 在windowless=true的条件下让silverlight输入中文
- IOS7 UI Transition Guide 状态栏 statusbar
- hdu1711 Number Sequence (KMP)
- MySQL key/value存储方案(转)
- 动态设置 UITextField
- db file sequential read等待事件
- 牛逼的Android UI--第18章 与用户交互相关的设计模式 (中)
- requestScope含义
- GUI.Window窗口
- zoj 3659 Conquer a New Region(并查集)
- hdu - 4396 More lumber is required(二维最短路)
- 前端面霸系列(1):doctype 、Quirks Mode & Standards Mode 、document.compatMode
- quick sort实现
- Sicily 1888. Circular Sequence
- 前端面霸系列(1):doctype 、Quirks Mode & Standards Mode 、document.compatMode
- uva 133 The Dole Queue
- GUI图形界面(2)