标签:
我们经常看到有些应用中会出现 “ 请稍等 ” 对话框,Android中ProgressDialog类可以实现该效果
O(∩_∩)O~
layout/activity_main.xml代码如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="20dp" > <Button android:id="@+id/circleButt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:text="圆形进度条" /> <Button android:id="@+id/longButt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="60dp" android:text="长形进度条" /> </LinearLayout>
创建一个ProgressDialogExample类继承Activity
1 package com.example.lenovo.a1101; 2 3 import android.app.Activity; 4 import android.app.ProgressDialog; 5 import android.content.DialogInterface; 6 import android.os.Bundle; 7 import android.os.PersistableBundle; 8 import android.preference.DialogPreference; 9 import android.view.View; 10 import android.widget.Button; 11 import android.widget.Toast; 12 13 /** 14 * Created by Lenovo on 2015/11/1. 15 */ 16 public class ProgressDialogExample extends Activity { 17 Button circleBut; 18 Button longBut; 19 ProgressDialog myDialog; 20 21 @Override 22 public void onCreate(Bundle savedInstanceState) { 23 super.onCreate(savedInstanceState); 24 setContentView(R.layout.activity_main); 25 circleBut = (Button) findViewById(R.id.circleButt); 26 longBut = (Button) findViewById(R.id.longButt); 27 circleBut.setOnClickListener(new View.OnClickListener() { 28 @Override 29 public void onClick(View v) { 30 myDialog = new ProgressDialog(ProgressDialogExample.this); 31 //创建progressDialog对象 32 myDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 33 //设置进度条为圆形样式 34 myDialog.setTitle("提示"); 35 //设置进度条的标题信息 36 myDialog.setMessage("主人,小白正在努力加载中……"); 37 //设置进度条的提示信息 38 myDialog.setIcon(R.drawable.ic_launch); 39 //是指进度条的图标 40 myDialog.setIndeterminate(false); 41 //设置进度条是否为不明确 42 myDialog.setCancelable(true); 43 //为进度条添加"确定"按钮,并添加点击事件 44 myDialog.setButton("确定", new DialogInterface.OnClickListener() { 45 @Override 46 public void onClick(DialogInterface dialog, int which) { 47 myDialog.cancel(); 48 } 49 }); 50 myDialog.show(); 51 } 52 }); 53 longBut.setOnClickListener(new View.OnClickListener() { 54 int count = 0;//存储进度条当前的进度值,初始值为0 55 56 @Override 57 public void onClick(View v) { 58 59 myDialog = new ProgressDialog(ProgressDialogExample.this); 60 myDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 61 myDialog.setTitle("提示"); 62 myDialog.setMessage("快马加鞭加载中……"); 63 myDialog.setIcon(R.drawable.ic_launch); 64 myDialog.setIndeterminate(false); 65 myDialog.setCancelable(true); 66 myDialog.setMax(100); 67 //设置进度条的最大值 68 myDialog.setProgress(0); 69 //默认值为0 70 myDialog.setSecondaryProgress(50); 71 //设置第二进度条的长度 72 myDialog.setButton("取消", new DialogInterface.OnClickListener() { 73 74 @Override 75 public void onClick(DialogInterface dialog, int which) { 76 myDialog.cancel(); 77 } 78 79 }); 80 myDialog.show();//显示进度条 81 new Thread() { //定义线程,动态改变当前进度条的值 82 public void run() { 83 while (count <= 100) { 84 myDialog.setProgress(count++); 85 try { 86 Thread.sleep(100); 87 } catch (InterruptedException e) { 88 e.printStackTrace(); 89 } 90 } 91 92 } 93 94 }.start(); 95 96 } 97 }); 98 } 99 }
然而图标没有显示出来,明天再找问题所在! 各位路过的小伙伴可以帮帮忙!嘿嘿~(*^__^*) ……
标签:
原文地址:http://www.cnblogs.com/Lynn0814/p/4928709.html