Android开发之UI更新交互机制与实例解析
android开发过程中,经常需要更新UI的状态和文案等。这是就需要对UI进行 更新。在android中更新UI一般有三种方法,handler机制、RunOnUiThread方法以及AsyncTask异步类方法等
本文下面就这三种方法进行了演示和代码实现.
a.Handler机制通过使用消息机制来实现
b.RunOnUiThread方法是通过运行UI线程来达到更新UI的目的
c.AsyncTask是异步类,通过异步更新来更新UI
效果图如下:
(1)Java功能实现代码如下:
package com.czm.updateui; import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.R.integer;
import android.app.Activity;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView; public class UpdateUIActivity extends Activity { private Button btnHandler;
private Button btnRunOnUiThread;
private Button btnAsyncTask;
private TextView tvHandler;
private TextView tvOnUiThread;
private TextView tvProgress;
private Handler uiHandler;
private ProgressBar progressBar; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.update_ui);
initViews();
setListeners();
}
private void initViews(){
btnHandler = (Button) findViewById(R.id.btnHandler);
btnRunOnUiThread = (Button) findViewById(R.id.btnRunOnUiThread);
btnAsyncTask = (Button)findViewById(R.id.btnAsyncTask);
tvHandler = (TextView) findViewById(R.id.tvText1);
tvOnUiThread = (TextView)findViewById(R.id.tvText2);
tvProgress = (TextView)findViewById(R.id.tvText3);
progressBar = (ProgressBar)findViewById(R.id.progressBar);
}
private void setListeners(){
uiHandler = new Handler(){ @Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
Bundle bundle = msg.getData();
String text = bundle.getString("handler_text");
String color = bundle.getString("handler_color"); tvHandler.setText(text);
tvHandler.setBackgroundColor(Color.BLUE); } };
//通过Handler机制来更新UI
btnHandler.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
new Thread(){ @Override
public void run() {
// TODO Auto-generated method stub
Message msg =new Message();
Bundle bundle = new Bundle();
bundle.putString("handler_text", "我是由Handler更新UI后的文案");
bundle.putString("handler_color", "#0000FF");
msg.setData(bundle);
//uiHandler.sendMessageDelayed(msg, 2000);
uiHandler.sendMessage(msg);
} }.start();
}
}); //通过RunOnUiThread来更新UI
btnRunOnUiThread.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub runOnUiThread(new Runnable() { @Override
public void run() {
// TODO Auto-generated method stub
tvOnUiThread.setText("我是由runOnUiThread更新UI后的文案");
tvOnUiThread.setBackgroundColor(Color.RED); }
});
}
});
//通过AsyncTask 异步任务来更新UI
btnAsyncTask.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
new MyAsyncTask().execute(0);
}
});
} private class MyAsyncTask extends AsyncTask<Integer, Integer, Integer>{ @Override
protected void onPostExecute(Integer result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
tvProgress.setText("加载完成...100%");
} @Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
progressBar.setProgress((int)(values[0]));
tvProgress.setText("加载中..."+values[0]+"%");
} @Override
protected Integer doInBackground(Integer... params) {
// TODO Auto-generated method stub
Integer timer = 0;
while(timer <=100){
try {
publishProgress(timer);
timer ++;
Thread.sleep(40);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
} } }
(2)对应的UI布局xml文件代码:
<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"
android:gravity="center_horizontal"
tools:context=".UpdateUIActivity" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >"
<Button
android:id="@+id/btnHandler"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Handler" />
<Button
android:id="@+id/btnRunOnUiThread"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="runOnUiThread" />
<Button
android:id="@+id/btnAsyncTask"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AsyncTask" />
</LinearLayout> <TextView
android:id="@+id/tvText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Handler来更新UI"
android:padding="3dp"
android:textColor="#FFF"
android:background="#666666"
android:textSize="20dp" />
<TextView
android:id="@+id/tvText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="RunOnUiThread来更新UI"
android:padding="3dp"
android:textColor="#FFF"
android:background="#666666"
android:textSize="20dp" /> <ProgressBar android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="25dp"
android:progressDrawable="@drawable/progressbar"
android:max="100"
android:progress="0"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
/>
<TextView
android:id="@+id/tvText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="准备加载...0%"
android:textSize="20dp" /> </LinearLayout>
Android开发之UI更新交互机制与实例解析的更多相关文章
- Android开发之IPC进程间通信-AIDL介绍及实例解析
一.IPC进程间通信 IPC是进程间通信方法的统称,Linux IPC包括以下方法,Android的进程间通信主要采用是哪些方法呢? 1. 管道(Pipe)及有名管道(named pipe):管道可用 ...
- Android开发之UI的编程方式创建
我们知道,android中一个activity对应一个xml的UI配置文件,除了用xml文件配置的方式创建用户界面外,还可以使用代码编程的方式来创建一个用户界面.如果用户界面需要在运行过程中动态生成的 ...
- Android开发之Touch事件分发机制
原地址http://www.cnblogs.com/linjzong/p/4191891.html Touch事件分发中只有两个主角:ViewGroup和View.Activity的Touch事件事实 ...
- 【Android UI】Android开发之View的几种布局方式及实践
引言 通过前面两篇: Android 开发之旅:又见Hello World! Android 开发之旅:深入分析布局文件&又是“Hello World!” 我们对Android应用程序运行原理 ...
- Android开发之InstanceState详解
Android开发之InstanceState详解 本文介绍Android中关于Activity的两个神秘方法:onSaveInstanceState() 和 onRestoreInstanceS ...
- Android开发之bindService()侦听service内部状态
在Android开发之bindService()通信的基础上,实现bindService()方法侦听service内部状态. 实现侦听service内部状态,使用的是回调机制 1.首先实现一个接口 p ...
- Android开发之InstanceState详解(转)---利用其保存Activity状态
Android开发之InstanceState详解 本文介绍Android中关于Activity的两个神秘方法:onSaveInstanceState() 和 onRestoreInstanceS ...
- Android开发之旅4:应用程序基础及组件
引言 为了后面的例子做准备,本篇及接下来几篇将介绍Android应用程序的原理及术语,这些也是作为一个Android的开发人员必须要了解,且深刻理解的东西.本篇的主题如下: 1.应用程序基础 2.应用 ...
- Android开发之旅: Intents和Intent Filters(理论部分)
引言 大部分移动设备平台上的应用程序都运行在他们自己的沙盒中.他们彼此之间互相隔离,并且严格限制应用程序与硬件和原始组件之间的交互. 我们知道交流是多么的重要,作为一个孤岛没有交流的东西,一定毫无意义 ...
随机推荐
- HDU 4734
数位dp题:也是我做的第一个数位dp的题目: 感觉数位dp的模板性很强啊,思想都差不太多! 有几个写的很好的参考资料: 推荐一下: 数位计数问题解法研究 浅谈数位类统计问题 我的代码: #includ ...
- SDUT 2351 In Danger
点我看题目 题意 : 有n个兵想要自杀,所以他们决定围成一个圈,从1开始一直环到n,然后每第2个开始自杀,但是有一个兵不想死,所以让你编程求出最后一个应该死的人的位置,这样的话就剩他自己他可以不自杀了 ...
- POJ 2075 Tangled in Cables 最小生成树
简单的最小生成树,不过中间却弄了很久,究其原因,主要是第一次做生成树,很多细节不够熟练,find()函数的循环for判断条件是 pre[i]>=0,也就是遇到pre[i]==-1时停止,i就是并 ...
- IPVS实现分析
IPVS实现分析 IPVS实现分析 根据LVS官方网站的介绍,LVS支持三种负载均衡模式:NAT,tunnel和direct routing(DR). NAT是通用模式,所有交互数据必须通过均衡器:后 ...
- 【HDOJ】3277 Marriage Match III
Dinic不同实现的效率果然不同啊. /* 3277 */ #include <iostream> #include <string> #include <map> ...
- 【CF】323 Div2. D. Once Again...
挺有意思的一道题目.考虑长度为n的数组,重复n次,可以得到n*n的最长上升子序列.同理,也可以得到n*n的最长下降子序列.因此,把t分成prefix(上升子序列) + cycle(one intege ...
- pcDuino安装vnc进行远程控制
准备工作: 已经刷好的 pcduino : 点此购买 可选用显示器或者用ssh连接,ssh连接参考 无显示器刷机与使用 1.安装x11vnc 输入下面的命令: sudo apt-get insta ...
- Beta Round #9 (酱油杯noi考后欢乐赛)乌鸦喝水
题目:http://www.contesthunter.org/contest/Beta%20Round%20%EF%BC%839%20%28%E9%85%B1%E6%B2%B9%E6%9D%AFno ...
- [转]Unity 3D旋转矢量方向及二维平面基于一点选择另一点(Rotate a Vector3 direction & Rotate a point about another point in 2D )
http://specialwolf.blog.163.com/blog/static/124466832201301332432766/ ****************************** ...
- MD5Helper辅助类
DES加密和解密 public class MD5Helper { ///DES加密 ///sKey public string MD5Encrypt(string pToEncrypt, strin ...