Android开发更新UI的几种方式
2016-04-17 17:36
417 查看
1、runOnUiThread
2、handler post
3、handler sendmessage
4、view post
xml布局文件:
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.handlerui.MainActivity" >
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50sp"
android:text="@string/hello_world" />
</RelativeLayout>
MainActivity.java
2、handler post
3、handler sendmessage
4、view post
xml布局文件:
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.handlerui.MainActivity" >
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50sp"
android:text="@string/hello_world" />
</RelativeLayout>
MainActivity.java
package com.example.handlerui; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.view.Menu; import android.view.MenuItem; import android.widget.TextView; public class MainActivity extends Activity { private TextView textView; private Handler handler = new Handler(){ public void handleMessage(android.os.Message msg) { //textView.setText("OK"); //使用handler2更新UI的时候去掉注释
}; }; private void handler1(){ handler.post(new Runnable() { @Override public void run() { // TODO Auto-generated method stub textView.setText("OK"); } }); } private void handler2(){ handler.sendEmptyMessage(1); } private void updateUi(){ runOnUiThread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub textView.setText("OK"); } }); } private void viewUI(){ textView.post(new Runnable() { @Override public void run() { // TODO Auto-generated method stub textView.setText("OK"); } }); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.textView); new Thread(){ public void run() { try { Thread.sleep(2000); //handler1(); handler2(); //updateUi(); //viewUI(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }; }.start(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
相关文章推荐
- Request异步请求处理PART2
- (4.5.4.2)在Android Studio中进行单元测试和UI测试
- iOS学习笔记33-UICollectionView入门
- iOS学习笔记33-UICollectionView入门
- Android 屏幕尺寸、分辨率、像素密度等UI设计参考
- Penguins DbTools数据库管理移植差分工具(EXCEL相关) ver 20160417
- hdu-5667 Sequence(矩阵快速幂+费马小定理+快速幂)
- HDU 5667 Sequence
- easyui使用数据库数据展示导航标题:idea
- iOS开发——纯代码界面(UIViewController和文本类控件)
- UEditor富文本web编辑器
- IOS-UI设置字体属性
- (LeetCode 307) Range Sum Query - Mutable(树状数组讲解)
- NJUST 1925 sequence
- Android 在其他线程中更新UI线程的解决方法
- RequireJS源码初探
- RVO(Return Value Optimization)和NRVO(Named Return Value Optimization)
- hdu 5667 Sequence【费马小定理+矩阵快速幂】
- (LeetCode 307) Range Sum Query - Mutable(Segment Tree)
- hdu 5667 Sequence 矩阵快速幂