进度条对话框ProgressDialog

发布时间:2020-06-25 21:37:54 作者:没有水勒鱼
来源:网络 阅读:318

进度条对话框ProgressDialog经常用于不能马上完成的操作,为了避免用户莫名其妙的等待,给用户一个提示。

  本例中我们演示了两种进度条:条形进度条和圆形进度条。

一、设计界面

  1、打开“res/layout/activity_main.xml”文件。

  从工具栏向activity拖出2个按钮Button。

进度条对话框ProgressDialog


2、打开activity_main.xml文件。

  代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" >

   <Button
       android:id="@+id/progress"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@string/progress" />

   <Button
       android:id="@+id/circle"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@string/circle" />

</LinearLayout>


二、长按事件 

  打开“src/com.genwoxue.progress/MainActivity.java”文件。

  然后输入以下代码:  

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
//声明按钮
private Button btnCircle = null;
private Button btnProgress = null;
//声明进度条对话框
private ProgressDialog pdDialog = null;
//进度计数
int iCount = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取按钮对象
btnCircle = (Button) findViewById(R.id.circle);
btnProgress = (Button) findViewById(R.id.progress);
//设置btnCircle的事件监听
btnCircle.setOnClickListener(new OnClickListener() {

@SuppressWarnings("deprecation")
@Override
public void onClick(View v) {
iCount = 0;
//创建ProgressDialog对象
pdDialog = new ProgressDialog(MainActivity.this);
//设置进度条风格,风格为圆形,旋转的
pdDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
//设置ProgressDialog标题
pdDialog.setTitle("圆形进度条");
//设置ProgressDialog提示信息
pdDialog.setMessage("正在下载中……");
//设置ProgressDialog标题图标
pdDialog.setIcon(R.drawable.ic_launcher);
//设置ProgressDialog进度条进度
pdDialog.setProgress(100);
//设置ProgressDialog的进度条是否不明确
pdDialog.setIndeterminate(false);
//设置ProgressDialog是否可以按退回按键取消
pdDialog.setCancelable(true);
//设置ProgressDialog的一个Button
pdDialog.setButton("取消", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int i) {
//点击“取消”按钮取消对话框
dialog.cancel();
}
});
//让ProgressDialog显示
pdDialog.show();
//创建线程实例
new Thread(){
public void run(){
try{
while(iCount<=100){
//由线程来控制进度
pdDialog.setProgress(iCount++);
Thread.sleep(50);
}
pdDialog.cancel();
}catch(InterruptedException e){
pdDialog.cancel();
}
}
}.start();
}
});
//设置btnProgress的事件监听
btnProgress.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
iCount = 0;
//创建ProgressDialog对象
pdDialog = new ProgressDialog(MainActivity.this);
//设置进度条风格,风格为长形
pdDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//设置ProgressDialog标题
pdDialog.setTitle("条形进度条");
//设置ProgressDialog提示信息
pdDialog.setMessage("正在下载中……");
//设置ProgressDialog标题图标
pdDialog.setIcon(R.drawable.ic_launcher);
//设置ProgressDialog进度条进度
pdDialog.setProgress(100);
//设置ProgressDialog的进度条是否不明确
pdDialog.setIndeterminate(false);
//设置ProgressDialog是否可以按退回按键取消
pdDialog.setCancelable(true);
//让ProgressDialog显示
pdDialog.show();
//创建线程实例
new Thread(){
public void run(){
try{
while(iCount<=100){
//由线程来控制进度
pdDialog.setProgress(iCount++);
Thread.sleep(50);
}
pdDialog.cancel();
}catch(InterruptedException e){
pdDialog.cancel();
}
}
}.start();
}
});
}

}


三、运行效果

进度条对话框ProgressDialog进度条对话框ProgressDialog



推荐阅读:
  1. ProgressDialog 简单使用记录
  2. ProgressDialog

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

对话框 进度条 layout

上一篇:[Python] 程序结构与控制流

下一篇:光猫手机自动激活系统-开发指南-004- OLT添加vlan(ADD- VLAN)

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》