面试常客Handler详细解析(更新UI的几种方式)(六)
2016-03-07 09:39
561 查看
一共有:
UI主线程 activityd的runOnUiThread
handler post
handler sendMessage
view post
下面将我自己已经验证成功的代码贴出了,其实这些方法都是殊途同归,都是使用了handler,封装成message进行发送的:
主程序:
布局文件:
UI主线程 activityd的runOnUiThread
handler post
handler sendMessage
view post
下面将我自己已经验证成功的代码贴出了,其实这些方法都是殊途同归,都是使用了handler,封装成message进行发送的:
主程序:
package com.example.handler; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.widget.TextView; public class FiveActivity extends Activity { private TextView tv; private Handler handler = new Handler(){ public void handleMessage(android.os.Message msg) { tv.setText("ok2"); }; }; private void handle1(){ handler.post(new Runnable() { @Override public void run() { tv.setText("ok"); } }); } private void handle2(){ handler.sendEmptyMessage(1); } private void updateUI(){ runOnUiThread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub tv.setText("ok3"); } }); } private void viewUI(){ tv.post(new Runnable() { @Override public void run() { // TODO Auto-generated method stub tv.setText("ok4"); } }); } @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.fiva); tv = (TextView) findViewById(R.id.tv); new Thread(){ public void run() { try { Thread.sleep(2000); viewUI(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }; }.start();; } }
布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv" android:textSize="40sp"/> </LinearLayout>
相关文章推荐
- 使用uicollectionview 实现单元格滑动吸附效果
- 用WebRequest +HtmlAgilityPack 从外网抓取数据到本地
- easyui input 赋值、取值
- android开发游记:百度地图SDK路线规划和自定义地图UI覆盖物及弹出窗
- YOUTH(年轻)——Samuel Ullman(塞缪尔·乌尔曼)
- page、request、session和application区别 及各自作用域
- UESTC - 1251 谕神的密码 (模拟) 水
- 【DataStructure】Description and usage of queue
- 机房收费系统合作版(三)——UI思索
- POJ 3458 Colour Sequence
- UESTC - 1252 24点游戏 (DFS)好题
- UESTC - 1253 阿里巴巴和n个大盗 (博弈)
- poj 2031 Building a Space Station
- Easyui弹出Dialog时,Dialog中的Combobox的url会加载两次,不明原因,只能其他方法解决。
- iOS报错之this class is not key value coding-compliant for the key digitPressed.
- UIScrollView
- UIPikerView的属性
- cf#94-div1-A. Statues
- UIPageControl
- UI控件--UILabel