效果:
[img]http://dl.iteye.com/upload/attachment/384581/2a432d77-a9c8-33b7-b0ee-f9973e5d4dbc.jpg[/img]
[img]http://dl.iteye.com/upload/attachment/384583/700df879-3086-3058-91ea-460fa4ca2fe0.jpg[/img]
main.xml
strings.xml
[img]http://dl.iteye.com/upload/attachment/384581/2a432d77-a9c8-33b7-b0ee-f9973e5d4dbc.jpg[/img]
[img]http://dl.iteye.com/upload/attachment/384583/700df879-3086-3058-91ea-460fa4ca2fe0.jpg[/img]
main.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<Button
android:id="@+id/loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是个加载按钮"
android:layout_x="97px"
android:layout_y="187px"
>
</Button>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="24px"
android:text="TextView"
android:layout_x="116px"
android:layout_y="128px"
>
</TextView>
</AbsoluteLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, LoadingTest!</string>
<string name="dialog_title">请稍等片刻..</string>
<string name="dialog_messge">加载中...</string>
<string name="app_name">LoadingTest</string>
</resources>
package londing.test;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class LoadingTest extends Activity {
/** 在Android里是通过ProgressDialog来运行加载对话框,但留意的是 Android的ProgressDialog必须在
*后台程序运行完毕之前以dismiss()方法来关闭取得(focus)焦点,否则成寻会陷入无限循环无法终止程序,
*或者在线程里不可有任何的更改Context或paren View的任何状态,文字输出等事件。*/
private TextView text=null;
/**声明一个ProgressDialog*/
public ProgressDialog myDialog=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**载入main.xml*/
setContentView(R.layout.main);
/**通过ID找到Button组件*/
Button bt=(Button)findViewById(R.id.loading);
/**设置Button按钮点击事件*/
bt.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
/**设置ProgressDialog标题文字*/
final CharSequence title=getString(R.string.dialog_title);
/**设置ProgressDialog消息文字*/
final CharSequence messge=getString(R.string.dialog_messge);
/**设置ProgressDialog消息,标题文字显示*/
myDialog=ProgressDialog.show(LoadingTest.this, title, messge,true);
/**通过ID找到TextView组件*/
text=(TextView)findViewById(R.id.text);
/**设置TextView文字*/
text.setText(messge);
new Thread(){
public void run(){
try{
/**线程睡眠*/
sleep(3000);
}catch(Exception e){
e.printStackTrace();
}finally{
/**关闭ProgressDialog*/
myDialog.dismiss();
}
}
/**开启线程*/
}.start();
}
});
}
}