Android之ProgressDialog的使用例子
本例是点击下载按钮,弹出进度条对话框,等待3秒后关闭,模拟提交数据保存成功之前等待的功能。
解决方法:
1、activity_main.xml源码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.ff.MainActivity" >
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="84dp"
android:text="点击下载" />
</RelativeLayout>
2、MainActivity.java源码
package com.example.ff;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
public class MainActivity extends Activity {
Button btnDownload;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnDownload = (Button) findViewById(R.id.button);
btnDownload.setOnClickListener(listener);
}
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
//ProgressDialog是AlertDialog的子类,显示进度对话框,直接使用show方法
final ProgressDialog dialog = ProgressDialog.show(MainActivity.this,
"下载", "正在下载,请稍后...");
new Thread(){
@Override
public void run() {
try {
sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
dialog.dismiss();
}
}.start();
}
};
}
本文地址:http://www.yayihouse.com/yayishuwu/chapter/1146