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();
}
}

更多相关文章

  1. Android中的对话框
  2. Android 常用dialog提示对话框
  3. Android webview监听网页对话框点击事件
  4. 第32章、日期对话框DatePickerDialog(从零开始学Android)
  5. Android 自定义布局对话框避免全屏的设置
  6. Android关闭开机弹出SIM卡变动提示对话框
  7. Android中对话框的工具类
  8. Android全屏对话框(附带延时关闭效果)
  9. android创建自定义对话框

随机推荐

  1. android selector 无效的问题
  2. Android(安卓)Studio添加jar包
  3. Android(安卓)广播的发送与接收
  4. 转:android 线控按钮编程
  5. android 双卡双待 发送短信
  6. Android 学习笔记之SurfaceView的使用+如
  7. Android(安卓)Activity的显示过程简述
  8. Android获取手机通话记录的方法
  9. android选择和裁剪图像拍摄的图像
  10. Android Animation 动画解析