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(理论部分)
引言 大部分移动设备平台上的应用程序都运行在他们自己的沙盒中.他们彼此之间互相隔离,并且严格限制应用程序与硬件和原始组件之间的交互. 我们知道交流是多么的重要,作为一个孤岛没有交流的东西,一定毫无意义 ...
随机推荐
- 【技术贴】三星Note8 N5100实用教程,关闭相机快门声,增加浏览器退出按钮。
需要root 增加快门声按钮: 在\system\csc\目录下,有个others.xml的手机功能定制文件,用root explorer之类可以修改系统文件权限的文本修改工具编辑它,在文件最末添加这 ...
- 【技术贴】解决 myeclipse打不开报错an error has occurred, see .
方法1.右键选中快捷方式属性选项,在快捷方式页,目标一项最后加上-clean选项,如C:\MyEclipse6\eclipse.exe -clean. 然后重新启动一下MyEclipse. 方法2. ...
- ThinkPHP3.1快速入门(3)查询语言
http://www.thinkphp.cn/info/115.html 介绍 ThinkPHP内置了非常灵活的查询方法,可以快速的进行数据查询操作,查询条件可以用于读取.更新和删除等操作,主要涉及到 ...
- Being a Hero
zoj3241:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3241 题意:一个国家的地图是一张n个点m条边的有向图.你保 ...
- SDUT 2351 In Danger
点我看题目 题意 : 有n个兵想要自杀,所以他们决定围成一个圈,从1开始一直环到n,然后每第2个开始自杀,但是有一个兵不想死,所以让你编程求出最后一个应该死的人的位置,这样的话就剩他自己他可以不自杀了 ...
- Seattle(65) lypzxy的博客
http://www.cnblogs.com/cb168/tag/Firemonkey/
- 【Xamarin挖墙脚系列:学习资料大放送】
原文:[Xamarin挖墙脚系列:学习资料大放送] 最靠谱的还是官方的文档,英文的,借着翻译工具,硬看吧.还能学习英文........... https://developer.xamarin.com ...
- Android 实现ListView异步加载图片
ListView异步加载图片是非常实用的方法,凡是是要通过网络获取图片资源一般使用这种方法比较好,用户体验好,下面就说实现方法,先贴上主方法的代码: package cn.wangmeng.test; ...
- 【HDOJ】1160 FatMouse's Speed
DP. #include <stdio.h> #include <string.h> #include <stdlib.h> #define MAXNUM 1005 ...
- 为web服务器设置HttpOnly防范XSS攻击
HttpOnly标识是一个可选的.避免利用XSS(Cross-Site Scripting)来获取session cookie的标识.XSS攻击最常见一个的目标是通过获取的session cookie ...