2013年2月8日星期五
Android高手进阶教程(九)之----Android Handler的使用!!!
大家好我们这一节讲的是Android Handler的使用,在讲Handler之前,我们先提个小问题,就是如何让程序5秒钟更新一下Title.
首先我们看一下习惯了Java编程的人,在不知道Handler的用法之前是怎么样写的程序,代码如下所示:
package com.android.tutor;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
public class HandlerDemo extends Activity {
//title为setTitle方法提供变量,这里为了方便我设置成了int型
private int title = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Timer timer = new Timer();
timer.scheduleAtFixedRate(new MyTask(), 1, 5000);
}
private class MyTask extends TimerTask{
@Override
public void run() {
setTitle("Welcome to Mr Wei's blog " + title);
title ++;
}
}
}
然而当我们执行程序,并不能达到我们预期的效果,所以Android 引进了Handler 这个特殊的类,可以说它是Runnable和Activity交互的桥梁
,所以我们只要在run方法中发送Message,而在Handler里,通过不同的Message执行不同的任务。
所以我们修改后的代码如下:
package com.android.tutor;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
public class HandlerDemo extends Activity {
//title为setTitle方法提供变量,这里为了方便我设置成了int型
private int title = 0;
private Handler mHandler = new Handler(){
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
updateTitle();
break;
}
};
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Timer timer = new Timer();
timer.scheduleAtFixedRate(new MyTask(), 1, 5000);
}
private class MyTask extends TimerTask{
@Override
public void run() {
Message message = new Message();
message.what = 1;
mHandler.sendMessage(message);
}
}
public void updateTitle(){
setTitle("Welcome to Mr Wei's blog " + title);
title ++;
}
}
Android 基础教程之-------Android ProgressBar的使用!
大家好,今天给大家讲一下Android中ProgressBar的使用,这个控件在开发中还是经常用到的,比如用户浏览网页的时候,中间肯定有个 传输过程,所以用进度条让用户耐心等待。再比如我在Android Market下载应用,它也会有进度条显示下载进度。我为什么要举这两个例子呢,是因为它们分别是圆形进度条和长形进度条的代表。如下面两张图:
为了让大家更容易理解我将简单写一个Demo,大家可以按照我的步骤一步一步来。
第一步:新建一个Android工程命名为ProgressBarDemo.
第二步:修改main.xml代码如下(圆形进度条和长形进度条这里样式不同用系统自带的):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Welcome to Mr Wei's blog" />
<ProgressBar
android:id="@+id/rectangleProgressBar"
style="?android:attr/progressBarStyleHorizontal" mce_style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone"
/>
<ProgressBar
android:id="@+id/circleProgressBar"
style="?android:attr/progressBarStyleLarge" mce_style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
/>
<Button android:id="@+id/button"
android:text="Show ProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
第三步:修改ProgressBarDemo.java代码如下(如果这里有对Handler和Message不明白的地方可以去Android高 手进阶教程(九) 看下这个教程):
[c-sharp] view plaincopy
package com.tutor.progress;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
public class ProgressBarDemo extends Activity {
private ProgressBar rectangleProgressBar,circleProgressBar;
private Button mButton;
protected static final int STOP = 0x10000;
protected static final int NEXT = 0x10001;
private int iCount = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//findView By id
rectangleProgressBar = (ProgressBar)findViewById(R.id.rectangleProgressBar);
circleProgressBar = (ProgressBar)findViewById(R.id.circleProgressBar);
mButton = (Button)findViewById(R.id.button);
rectangleProgressBar.setIndeterminate(false);
circleProgressBar.setIndeterminate(false);
mButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
rectangleProgressBar.setVisibility(View.VISIBLE);
circleProgressBar.setVisibility(View.VISIBLE);
rectangleProgressBar.setMax(100);
rectangleProgressBar.setProgress(0);
circleProgressBar.setProgress(0);
//创建一个线程,每秒步长为5增加,到100%时停止
Thread mThread = new Thread(new Runnable() {
public void run() {
for(int i=0 ; i < 20; i++){
try{
iCount = (i + 1) * 5;
Thread.sleep(1000);
if(i == 19){
Message msg = new Message();
msg.what = STOP;
mHandler.sendMessage(msg);
break;
}else{
Message msg = new Message();
msg.what = NEXT;
mHandler.sendMessage(msg);
}
}catch (Exception e) {
e.printStackTrace();
}
}
}
});
mThread.start();
}
});
}
//定义一个Handler
private Handler mHandler = new Handler(){
public void handleMessage(Message msg){
switch (msg.what) {
case STOP:
rectangleProgressBar.setVisibility(View.GONE);
circleProgressBar.setVisibility(View.GONE);
Thread.currentThread().interrupt();
break;
case NEXT:
if(!Thread.currentThread().isInterrupted()){
rectangleProgressBar.setProgress(iCount);
circleProgressBar.setProgress(iCount);
}
break;
}
}
};
}
第四步:运行上述工程,效果如下图:
图1.UC浏览网页圆形进度条效果。
图2.Google Market应用下载长形进度条效果.
下面我们看一下两都皆有之的Android自带的浏览器的效果图如下:
为了让大家更容易理解我将简单写一个Demo,大家可以按照我的步骤一步一步来。
第一步:新建一个Android工程命名为ProgressBarDemo.
第二步:修改main.xml代码如下(圆形进度条和长形进度条这里样式不同用系统自带的):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Welcome to Mr Wei's blog" />
<ProgressBar
android:id="@+id/rectangleProgressBar"
style="?android:attr/progressBarStyleHorizontal" mce_style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone"
/>
<ProgressBar
android:id="@+id/circleProgressBar"
style="?android:attr/progressBarStyleLarge" mce_style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
/>
<Button android:id="@+id/button"
android:text="Show ProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
第三步:修改ProgressBarDemo.java代码如下(如果这里有对Handler和Message不明白的地方可以去Android高 手进阶教程(九) 看下这个教程):
[c-sharp] view plaincopy
package com.tutor.progress;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
public class ProgressBarDemo extends Activity {
private ProgressBar rectangleProgressBar,circleProgressBar;
private Button mButton;
protected static final int STOP = 0x10000;
protected static final int NEXT = 0x10001;
private int iCount = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//findView By id
rectangleProgressBar = (ProgressBar)findViewById(R.id.rectangleProgressBar);
circleProgressBar = (ProgressBar)findViewById(R.id.circleProgressBar);
mButton = (Button)findViewById(R.id.button);
rectangleProgressBar.setIndeterminate(false);
circleProgressBar.setIndeterminate(false);
mButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
rectangleProgressBar.setVisibility(View.VISIBLE);
circleProgressBar.setVisibility(View.VISIBLE);
rectangleProgressBar.setMax(100);
rectangleProgressBar.setProgress(0);
circleProgressBar.setProgress(0);
//创建一个线程,每秒步长为5增加,到100%时停止
Thread mThread = new Thread(new Runnable() {
public void run() {
for(int i=0 ; i < 20; i++){
try{
iCount = (i + 1) * 5;
Thread.sleep(1000);
if(i == 19){
Message msg = new Message();
msg.what = STOP;
mHandler.sendMessage(msg);
break;
}else{
Message msg = new Message();
msg.what = NEXT;
mHandler.sendMessage(msg);
}
}catch (Exception e) {
e.printStackTrace();
}
}
}
});
mThread.start();
}
});
}
//定义一个Handler
private Handler mHandler = new Handler(){
public void handleMessage(Message msg){
switch (msg.what) {
case STOP:
rectangleProgressBar.setVisibility(View.GONE);
circleProgressBar.setVisibility(View.GONE);
Thread.currentThread().interrupt();
break;
case NEXT:
if(!Thread.currentThread().isInterrupted()){
rectangleProgressBar.setProgress(iCount);
circleProgressBar.setProgress(iCount);
}
break;
}
}
};
}
第四步:运行上述工程,效果如下图:
订阅:
博文 (Atom)