<!-- 正文开始 -->
在Android中,要实现Loading效果,一般情况下都使用ProgressDialog控件。ApiDemos/src/com/example/android/apis/view/ProgressBar3.java 提供两个demo:
仔细看了Android Market,发现却是不一样的,请看截图:
那到底如何实现呢?首先,我们创建一个布局文件,
res/layout/fullscreen_loading_indicator.xml, 其内容如下:
<?
xml version
=
"
1.0
"
encoding
=
"
utf-8
"
?>
<
LinearLayout
xmlns:android
=
"
http://schemas.android.com/apk/res/android
"
android:gravity
=
"
center_vertical|center_horizontal
"
android:orientation
=
"
horizontal
"
android:id
=
"
@+id/fullscreen_loading_style
"
android:layout_width
=
"
fill_parent
"
android:layout_height
=
"
fill_parent
"
>
<
ProgressBar
android:layout_gravity
=
"
center_vertical
"
android:layout_width
=
"
wrap_content
"
android:layout_height
=
"
wrap_content
"
style
=
"
?android:attr/progressBarStyleSmall
"
/>
<
TextView
android:id
=
"
@+id/current_action
"
android:layout_width
=
"
wrap_content
"
android:layout_height
=
"
wrap_content
"
android:layout_marginLeft
=
"
5.0dip
"
android:text
=
"
@+string/loading
"
/>
</
LinearLayout
>
然后在main.xml 把它include 进来
<?
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
"
>
<
LinearLayout android:orientation
=
"
vertical
"
android:id
=
"
@+id/main_info
"
android:visibility
=
"
gone
"
android:layout_width
=
"
fill_parent
"
android:layout_height
=
"
fill_parent
"
>
<
TextView android:layout_width
=
"
fill_parent
"
android:layout_height
=
"
fill_parent
"
android:text
=
"
abc
"
></
TextView
>
</
LinearLayout
>
<
LinearLayout android:orientation
=
"
vertical
"
android:id
=
"
@+id/main_Loading
"
android:layout_width
=
"
fill_parent
"
android:layout_height
=
"
fill_parent
"
>
<
include android:visibility
=
"
visible
"
android:layout_width
=
"
fill_parent
"
android:layout_height
=
"
fill_parent
"
layout
=
"
@layout/fullscreen_loading_style
"
/>
</
LinearLayout
>
</
LinearLayout
>
主程序:
package
com.tymx.fullloading;
import
android.app.Activity;
import
android.os.Bundle;
import
android.os.Handler;
import
android.os.Message;
import
android.view.View;
import
android.widget.LinearLayout;
public
class
myFullLoading
extends
Activity {
private
LinearLayout mLoadingLayout;
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
final
Handler handler
=
new
Handler(){
@Override
public
void
handleMessage(Message msg) {
//
TODO Auto-generated method stub
super
.handleMessage(msg);
if
(msg.what
==
1
){
mLoadingLayout
=
(LinearLayout)findViewById(R.id.fullscreen_loading_style);
mLoadingLayout.setVisibility(View.GONE);
LinearLayout mMain
=
(LinearLayout)findViewById(R.id.main_info);
mMain.setVisibility(View.VISIBLE);
}
}
};
new
Thread(){
public
void
run(){
for
(
int
i
=
0
;i
<
1000
;i
++
){
System.out.print(i
+
""
);
}
Message msg
=
handler.obtainMessage(
1
,
"
flash
"
);
handler.sendMessage(msg);
}
}.start();
}
}
运行的效果为:
public class ProgressBar3 extends Activity {
ProgressDialog mDialog1;
ProgressDialog mDialog2;
private static final int DIALOG1_KEY = 0;
private static final int DIALOG2_KEY = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.progressbar_3);
Button button = (Button) findViewById(R.id.showIndeterminate);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(DIALOG1_KEY);
}
});
button = (Button) findViewById(R.id.showIndeterminateNoTitle);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(DIALOG2_KEY);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG1_KEY: {
ProgressDialog dialog = new ProgressDialog(this);
dialog.setTitle("Indeterminate");
dialog.setMessage("Please wait while loading...");
dialog.setIndeterminate(true);
dialog.setCancelable(true);
return dialog;
}
case DIALOG2_KEY: {
ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage("Please wait while loading...");
dialog.setIndeterminate(true);
dialog.setCancelable(true);
return dialog;
}
}
return null;
}
}
package com.yarin.android.TestOnPDialog;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class PDialog extends Activity
{
private Button mButton01,mButton02;
int m_count = 0;
//声明进度条对话框
ProgressDialog m_pDialog;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//得到按钮对象
mButton01 = (Button)findViewById(R.id.Button01);
mButton02 = (Button)findViewById(R.id.Button02);
//设置mButton01的事件监听
mButton01.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
//创建ProgressDialog对象
m_pDialog = new ProgressDialog(PDialog.this);
// 设置进度条风格,风格为圆形,旋转的
m_pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
// 设置ProgressDialog 标题
m_pDialog.setTitle("提示");
// 设置ProgressDialog 提示信息
m_pDialog.setMessage("这是一个圆形进度条对话框");
// 设置ProgressDialog 标题图标
m_pDialog.setIcon(R.drawable.img1);
// 设置ProgressDialog 的进度条是否不明确
m_pDialog.setIndeterminate(false);
// 设置ProgressDialog 是否可以按退回按键取消
m_pDialog.setCancelable(true);
// 设置ProgressDialog 的一个Button
m_pDialog.setButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int i)
{
//点击“确定按钮”取消对话框
dialog.cancel();
}
});
// 让ProgressDialog显示
m_pDialog.show();
}
});
//设置mButton02的事件监听
mButton02.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
m_count = 0;
// 创建ProgressDialog对象
m_pDialog = new ProgressDialog(PDialog.this);
// 设置进度条风格,风格为长形
m_pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
// 设置ProgressDialog 标题
m_pDialog.setTitle("提示");
// 设置ProgressDialog 提示信息
m_pDialog.setMessage("这是一个长形对话框进度条");
// 设置ProgressDialog 标题图标
m_pDialog.setIcon(R.drawable.img2);
// 设置ProgressDialog 进度条进度
m_pDialog.setProgress(100);
// 设置ProgressDialog 的进度条是否不明确
m_pDialog.setIndeterminate(false);
// 设置ProgressDialog 是否可以按退回按键取消
m_pDialog.setCancelable(true);
// 让ProgressDialog显示
m_pDialog.show();
new Thread()
{
public void run()
{
try
{
while (m_count <= 100)
{
// 由线程来控制进度。
m_pDialog.setProgress(m_count++);
Thread.sleep(100);
}
m_pDialog.cancel();
}
catch (InterruptedException e)
{
m_pDialog.cancel();
}
}
}.start();
}
});
}
}