两种进度条对话框
xml
----------------
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" tools:context=".MainActivity">
<Button android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="显示进度对话框"
/>
<Button android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="显示旋转指针对话框"
/>
</LinearLayout >
java
-------------------
package com.example.huanglifeng.myapp2;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;
/**
* Created by huanglifeng on 2015/7/8.
*/
public class ActivityLifeCycle extends Activity implements View.OnClickListener{
private static final int MAX_PROGRESS=200;
private ProgressDialog progressDialog;
private Handler progressHandler;
private int progress;
private void showProgressDialog(int style)
{
progressDialog=new ProgressDialog(this);
progressDialog.setIcon(R.drawable.wait);
progressDialog.setTitle("正在处理数据...");
progressDialog.setMessage("请稍后");
progressDialog.setProgressStyle(style);
progressDialog.setMax(MAX_PROGRESS);
progressDialog.setButton(DialogInterface.BUTTON_POSITIVE,"暂停",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
progressHandler.removeMessages(1);
progress=0;
progressDialog.setProgress(0);
}
});
progressDialog.show();
progressHandler=new Handler(){
@Override
public void handleMessage(Message msg)
{
super.handleMessage(msg);
if(progress>=MAX_PROGRESS)
{
progress=0;
progressDialog.dismiss();
}else{
progress++;
progressDialog.incrementProgressBy(1);
progressHandler.sendEmptyMessageDelayed(1,50+new Random().nextInt(500));
}
}
};
progress=(progress>0)?progress:0;
progressDialog.setProgress(progress);
progressHandler.sendEmptyMessage(1);
}
@Override
public void onClick(View view)
{
switch (view.getId())
{
case R.id.button1:
showProgressDialog(ProgressDialog.STYLE_HORIZONTAL);
break;
case R.id.button2:
showProgressDialog(ProgressDialog.STYLE_SPINNER);
break;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1=(Button)findViewById(R.id.button1);
Button button2=(Button)findViewById(R.id.button2);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
}
@Override
protected void onRestart() {
super.onRestart();
}
@Override
protected void onStart() {
super.onStart();
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
更多相关文章
- Android中的对话框
- Android 常用dialog提示对话框
- Android webview监听网页对话框点击事件
- 第32章、日期对话框DatePickerDialog(从零开始学Android)
- Android 自定义布局对话框避免全屏的设置
- Android关闭开机弹出SIM卡变动提示对话框
- Android中对话框的工具类
- Android全屏对话框(附带延时关闭效果)
- android创建自定义对话框