Android主线程不能运行耗时操作。我们通常是在子线程中运行耗时操作,
我们在运行完耗时操作后,我们一般能够通过下面几种方式来实现ui界面的更新。 首先是布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/mTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="16sp" />
<Button
android:id="@+id/update_mButton_01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Hander Post"
android:textSize="15sp" />
<Button
android:id="@+id/update_mButton_02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Hander SendMessage"
android:textSize="15sp" />
<Button
android:id="@+id/update_mButton_03"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="RunOnUiThread"
android:textSize="15sp" />
<Button
android:id="@+id/update_mButton_04"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="View Post"
android:textSize="15sp" />
</LinearLayout>

————–代码实现,有凝视————————————-

public class MainActivity extends Activity implements OnClickListener {
private TextView mTextView;
private Button update_mButton_01;
private Button update_mButton_02;
private Button update_mButton_03;
private Button update_mButton_04;
private Handler mPostHander = new Handler() {
public void handleMessage(android.os.Message msg) {
if (msg.what == 1) {
// 更新UI
mTextView.setText("通过Hander Send Message更新Ui");
}
};
}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
initEvents();
} /**
* 初始化控件
* @description:
* @date 2015-10-8 上午10:55:49
*/
private void initViews() {
this.mTextView = (TextView) findViewById(R.id.mTextView);
this.update_mButton_01 = (Button) findViewById(R.id.update_mButton_01);
this.update_mButton_02 = (Button) findViewById(R.id.update_mButton_02);
this.update_mButton_03 = (Button) findViewById(R.id.update_mButton_03);
this.update_mButton_04 = (Button) findViewById(R.id.update_mButton_04);
} /**
* 事件监听
* @description:
* @date 2015-10-8 上午10:56:02
*/
private void initEvents() {
this.update_mButton_01.setOnClickListener(this);
this.update_mButton_02.setOnClickListener(this);
this.update_mButton_03.setOnClickListener(this);
this.update_mButton_04.setOnClickListener(this);
} @Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.update_mButton_01:// 第一种方式,通过Hander.post方法来实现UI更新
new Thread() {
public void run() {
try {
sleep(2000);// 休眠2秒,模拟耗时操作
mPostHander.post(new Runnable() { @Override
public void run() {
// 更新UI
mTextView.setText("通过Hander Post更新Ui");
}
});
}
catch (InterruptedException e) {
e.printStackTrace();
}
};
}.start();
break;
case R.id.update_mButton_02:// 直接通过Hander发送Message来更新UI
new Thread() {
public void run() {
try {
sleep(2000);// 休眠2秒。模拟耗时操作
mPostHander.sendEmptyMessage(1);
}
catch (InterruptedException e) {
e.printStackTrace();
}
};
}.start(); break;
case R.id.update_mButton_03:// 通过runOnUiThread来实现ui更新
new Thread() {
public void run() {
try {
sleep(2000);// 休眠2秒。模拟耗时操作
runOnUiThread(new Runnable() { @Override
public void run() {
// 更新UI
mTextView.setText("通过runOnUiThread更新Ui");
}
});
}
catch (InterruptedException e) {
e.printStackTrace();
}
};
}.start(); break;
case R.id.update_mButton_04:// 直接利用View.post方法来更新ui
mTextView.post(new Runnable() {
@Override
public void run() {
// 更新UI
mTextView.setText("通过View.post()更新Ui");
}
});
break;
}
}
}

最新文章

  1. ArchLinux+Win10双系统的Grub配置
  2. 崔用志-微信开发-java版本
  3. devexpress bandgridview使用总结(14.2)
  4. java练手 韩信点兵
  5. python解析xml模块封装代码
  6. 使用 autoconf
  7. [转载]获取当前日期和农历的js代码
  8. Android Activity整体管理和关闭工具类封装
  9. 【Flex】编辑器的缩放功能(绝对定位和相对定位)
  10. Linux系统 磁盘IO过高排查总结
  11. Javascript高级编程学习笔记(19)—— 对象属性
  12. ruby----controller中简单的增删改 方法定义
  13. iOS开发之--UIImageView的animationImages动画
  14. php yii 命令
  15. JAVA WebSocKet ( 简单的聊天室 )
  16. Laravel之Eloquent ORM
  17. OC - runtime - 1
  18. 那些ie6已支持的方法属性,成为事实标准。或者方便大家的api
  19. C语言一闪而过
  20. 从零打造在线网盘系统之Struts2框架核心功能全解析

热门文章

  1. [转]c# 对密码执行散列和 salt 运算方法
  2. 记Spring下autowire为name时的一个现象
  3. sql server 无法创建数据库,错误代码:1807
  4. js基础---数字日期及运算
  5. fcc html5 css 练习1
  6. CNN结构:色彩空间建模-色彩空间分析
  7. TensorFlow:Windows下使用TensorFlow-Python版本
  8. Centos6.7 ELK日志系统部署
  9. 数据库操作(二)SOQL
  10. vue中使用Swiper