进度条ProgressBar及ProgressDialog(实例)
作者:jingxian 发布时间:2021-09-24 01:07:42
标签:进度条,ProgressBar,ProgressDialog
废话不多说,直接上代码
Main代码
package processdemo.example.administrator.processbardemo;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
/*ProgressBar
简介:ProgressBar是进度条组件,通常用于向用户展示某个耗时操作完成的进度,而不让用户感觉是程序失去了响应,从而更好地提升用户界面的友好性
课程目标:
1、制定ProgressBar显示风格(系统默认)
2、ProgressBar的分类
水平进度条,能精确显示,圆圈进度条,不精确显示
3、标题上ProgressBar的设置
4、ProgressBar的关键属性
5、ProgressBar的关键方法
6、ProgressDiglog的基础使用
7、自定义ProgressBar样式*/
private ProgressBar progressBar3;
private Button show;
private Button add;
private Button res;
private Button reset;
private TextView textView;
private ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 启用窗口特征,启用带进度的进度条和不带进度的进度条,
requestWindowFeature(Window.FEATURE_PROGRESS);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_main);
setProgressBarVisibility(true);
setProgressBarIndeterminateVisibility(false);
// 最大值为Max=10000;
//setProgress(600);
init();
}
private void init() {
;
progressBar3= (ProgressBar) findViewById(R.id.progressBar3);
show= (Button) findViewById(R.id.show);
add= (Button) findViewById(R.id.add);
res= (Button) findViewById(R.id.res);
reset= (Button) findViewById(R.id.reset);
textView= (TextView) findViewById(R.id.textView);
int first=progressBar3.getProgress();/*获取第一进度*/
int second=progressBar3.getSecondaryProgress();/*获取第二进度*/
int max=progressBar3.getMax();/*获取最大进度*/
textView.setText("第一进度条百分比"+(int)((first/(float)max)*100)+"%"+"第二进度条百分比"+(int)(second/(float)max*100)+"%");
add.setOnClickListener(this);
res.setOnClickListener(this);
reset.setOnClickListener(this);
show.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.add:
progressBar3.incrementProgressBy(10);
progressBar3.incrementSecondaryProgressBy(10);
break;
case R.id.res:
progressBar3.incrementProgressBy(-10);
progressBar3.incrementSecondaryProgressBy(-10);
break;
case R.id.reset:
progressBar3.setProgress(50);
progressBar3.setSecondaryProgress(50);
break;
case R.id.show:
// 新建ProgressDialog对象
progressDialog=new ProgressDialog(MainActivity.this);
// 设置显示风格
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
// 设置标题
progressDialog.setTitle("慕课网");
// 设置对话框的内容
progressDialog.setMessage("欢迎大家支持慕课网");
// 设置图标
progressDialog.setIcon(R.mipmap.ic_launcher);
/*设置关于进度条的一些属性*/
// 设置最大进度
progressDialog.setMax(100);
// 设置初始化已经增长的进度
progressDialog.incrementProgressBy(50);
// 设置进度条明确显示进度
progressDialog.setIndeterminate(false);
/* 设定一个确定按钮*/
progressDialog.setButton(DialogInterface.BUTTON_POSITIVE,"确定", new Dialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"欢迎大家支持慕课网",Toast.LENGTH_SHORT).show();
}
});
// 是否可以通过返回按钮来取消对话框
progressDialog.setCancelable(true);
// 显示ProgressDialog
progressDialog.show();
}
textView.setText("第一进度条百分比"+(int)((progressBar3.getProgress()/(float)progressBar3.getMax())*100)+"%"+"第二进度条百分比"+(int)(progressBar3.getSecondaryProgress()/(float)progressBar3.getMax()*100)+"%");
}
}
layout中activity_main.xml代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_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="processdemo.example.administrator.processbardemo.MainActivity">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginTop="112dp" />
<ProgressBar
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar2"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true"
android:layout_marginEnd="256dp" />
<ProgressBar
style="@android:style/Widget.ProgressBar.Horizontal"
android:max="100"
android:progress="50"
android:secondaryProgress="80"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar3"
android:layout_alignParentBottom="true"
android:layout_alignStart="@+id/progressBar2"
android:layout_marginBottom="81dp"
android:layout_alignTop="@+id/res"
android:progressDrawable="@layout/progress"/><!--progressDrawable改变样式-->
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar4"
android:layout_above="@+id/reset"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/增加"
android:id="@+id/add"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/减少"
android:id="@+id/res"
android:layout_above="@+id/add"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/重置"
android:id="@+id/reset"
android:layout_alignBottom="@+id/progressBar3"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="进度"
android:id="@+id/textView"
android:layout_below="@+id/progressBar"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/显示进度条"
android:id="@+id/show"
android:layout_below="@+id/progressBar2"
android:layout_alignParentEnd="true" />
</RelativeLayout>
<!-- ProgressBar关键属性
1.android:max ---最大显示进度
2.android:progress ---第一显示进度
3.android:secondaryProgress---第二显示进度
4.android:isdeterminate ---设置是否精确显示(false为精确,true为不精确)-->
layout中progress.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<item android:id="@android:id/background">
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#76cf76"
android:centerColor="#125912"
android:centerY="0.75"
android:endColor="#212621"
android:angle="270"
/>
</shape>
</item>
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#80b51638"
android:centerColor="#80a47d1a"
android:centerY="0.75"
android:endColor="#a066c3a7"
android:angle="270"
/>
</shape>
</clip>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#a38c1c"
android:centerColor="#55a6b6"
android:centerY="0.75"
android:endColor="#b59826"
android:angle="270"
/>
</shape>
</clip>
</item>
</layer-list>


猜你喜欢
- 前言Java虽然五脏俱全但总有软肋,譬如获取CPU等硬件信息,当然我们可以通过JNI调用C/C++来获取,但对于对C/C++和Windows
- Http通信概述Http通信主要有两种方式POST方式和GET方式。前者通过Http消息实体发送数据给服务器,安全性高,数据传输大小没有限制
- 前言本文主要介绍了关于JDK源码分析之String、StringBuilder和StringBuffer的相关内容,分享出来供大家参考学习,
- 以前一直使用Hibernate,基本上没用过Mybatis,工作中需要做映射关系,简单的了解下Mybatis的映射。两者相差不多都支持一对一
- java Swing基础教程之图形化实例代码与多线程、泛型等不同,Swing主要在于使用。 下面主要放代码和注释,少说话。(一)
- 1、存储在App内部最简单的一种。在尝试过程中发现,手机中很多文件夹都没有权限读写。我们可以将我们需要写的文件存放到App中的files文件
- 1.使用API设置主题如下所示,在Activity中使用setThemesetTheme(R.style.MyTheme1);2.调用API
- //Main:using System;using System.Collections.Generic;using System.Linq
- 本文实例讲述了Android中资源文件用法。分享给大家供大家参考,具体如下:一、XML文件间资源文件的使用引用格式:attribute=&q
- C#之委托委托:顾名思义,让别人帮你办件事。委托是C#实现回调函数的一种机制。可能有人会问了,回调函数是个啥???举个例子:我现在是一家公司
- 在网上找到了一个可以防页面滚动滑到顶端或底部的时候, 漏出微信丑丑的灰色底,下面小编把核心代码分享给大家供大家参考!我的核心代码:preve
- 今天写一个小程序有一个给图片加上阴影的需求,记得WPF的Effect中就有阴影特效,就打算用它了。代码如下:using (var image
- FilterInputStream FilterInputStream 的作用是用来“封装其它的输入流,并为它们提供额外的功能”。它的常用的
- 概述由于微信公众平台的特殊机制,所有的信息都由微信服务器转发而来,因此服务器是无法使用Session对用户会话的上下文进行管理的。为此Sen
- 在实际项目当中,我们经常会涉及到对时间的处理,例如登陆网站,我们会看到网站首页显示XXX,欢迎您!今天是XXXX年。。。。某些网站会记录下用
- 前言最近在学习使用 React Native开发,iOS搞完,开始适配安卓,由于木有接触过安卓,所以碰到了很多问题,第一个问题,安卓的返回键
- 1.概述Spring MVC是Spring Framework的一部分,是基于Java实现MVC的轻量级Web框架。Spring MVC的特
- 一.总体设计1.寻找规律,公式化的生成坐标系。2.将生成坐标系的关键参数设置为可自定义,从而可变的可以生成自己想要的坐标系。3.将需要绘制的
- 原因是新建类的模板被更改了,匹配符变成了大写,并且没有默认修饰符。不想自己改的,粘上去就好了,不过我只加了默认的。#if (${PACKAG
- 1.背景Java语言相比于C和C++,一个最大的特点就是不需要程序员自己手动去申请和释放内存,这一切交由JVM来完成。在Java中,运行时的