使用ProgressBar显示进度条
2018-03-16 17:42
411 查看
此文,仅做为个人学习Android,记录成长以及方便复习!首先是设置UI界面1.activity_main.xml每个按钮都添加了 android:onClick="onclick" 方便使用监听方法,不用实例化按钮ProgressBar的几个常用参数style="?android:attr/progressBarStyleHorizontal" //设置显示样式,此为显示进度的样式
android:progress="50" //第一进度
android:secondaryProgress="80" //第二进度
android:max="100" //最大刻度
接下来就是Activity
MainActivity.javapackage com.rui.progressbardome;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private ProgressBar progressBar;//创建ProgressBar
private TextView tv1;//创建TextView
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();//调用init方法
}
public void init(){
progressBar = findView
4000
ById(R.id.progressBar);
tv1 = findViewById(R.id.tv1);
int max = progressBar.getMax();//获取进度最大值
int progress= progressBar.getProgress();//获取第一进度
int sdProgress= progressBar.getSecondaryProgress();//获取第二进度
//百分比是先把除数的整型转换成浮点型,相除的结果是一个0~1之间的小数
// 再乘以100得到0~100之间的小数,最后把这个小数转换成整数。
// 其实不用那么麻烦,改成 100*progress/max 和 100*sdProgress/max 就可以了:
tv1.setText("第一进度百分比:"+(int)((progress/(float)max)*100)+"% 第二进度百分百:"+(int)((sdProgress/(float)max)*100));
}
//按钮监听事件
public void onclick(View view){
switch(view.getId()){
case R.id.add:
//第一进度增加10
progressBar.incrementProgressBy(10);
//第二进度增加10
progressBar.incrementSecondaryProgressBy(10);
break;
case R.id.reduction:
//第一进度减少10
progressBar.incrementProgressBy(-10);
//第二进度减少10
progressBar.incrementSecondaryProgressBy(-10);
break;
case R.id.reset:
//设置第一进度
progressBar.setProgress(50);
//设置第二进度
progressBar.setSecondaryProgress(80);
break;
}
//重新获取进度并在tv1显示!
tv1.setText("第一进度百分比:"+progressBar.getProgress()+"% 第二进度百分百:"+progressBar.getSecondaryProgress());
}
}
运行后,单击增加按钮2次 效果如下
单击减少按钮1次 效果如下
点击重置按钮如下
android:progress="50" //第一进度
android:secondaryProgress="80" //第二进度
android:max="100" //最大刻度
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ProgressBar android:id="@+id/progressBar" style="?android:attr/progressBarStyleHorizontal" android:progress="50" android:secondaryProgress="80" android:max="100" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="增加10%" android:onClick="onclick" android:layout_below="@+id/progressBar"/> <Button android:id="@+id/reduction" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="减加10%" android:onClick="onclick" android:layout_below="@+id/add"/> <Button android:id="@+id/reset" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="重置" android:onClick="onclick" android:layout_below="@+id/reduction"/> <TextView android:id="@+id/tv1" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#ff0000" android:textSize="20sp" android:layout_below="@+id/reset"/> </RelativeLayout>
接下来就是Activity
MainActivity.javapackage com.rui.progressbardome;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private ProgressBar progressBar;//创建ProgressBar
private TextView tv1;//创建TextView
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();//调用init方法
}
public void init(){
progressBar = findView
4000
ById(R.id.progressBar);
tv1 = findViewById(R.id.tv1);
int max = progressBar.getMax();//获取进度最大值
int progress= progressBar.getProgress();//获取第一进度
int sdProgress= progressBar.getSecondaryProgress();//获取第二进度
//百分比是先把除数的整型转换成浮点型,相除的结果是一个0~1之间的小数
// 再乘以100得到0~100之间的小数,最后把这个小数转换成整数。
// 其实不用那么麻烦,改成 100*progress/max 和 100*sdProgress/max 就可以了:
tv1.setText("第一进度百分比:"+(int)((progress/(float)max)*100)+"% 第二进度百分百:"+(int)((sdProgress/(float)max)*100));
}
//按钮监听事件
public void onclick(View view){
switch(view.getId()){
case R.id.add:
//第一进度增加10
progressBar.incrementProgressBy(10);
//第二进度增加10
progressBar.incrementSecondaryProgressBy(10);
break;
case R.id.reduction:
//第一进度减少10
progressBar.incrementProgressBy(-10);
//第二进度减少10
progressBar.incrementSecondaryProgressBy(-10);
break;
case R.id.reset:
//设置第一进度
progressBar.setProgress(50);
//设置第二进度
progressBar.setSecondaryProgress(80);
break;
}
//重新获取进度并在tv1显示!
tv1.setText("第一进度百分比:"+progressBar.getProgress()+"% 第二进度百分百:"+progressBar.getSecondaryProgress());
}
}
运行后,单击增加按钮2次 效果如下
单击减少按钮1次 效果如下
点击重置按钮如下
相关文章推荐
- 使用ASP.NET Atlas编写显示真实进度的ProgressBar
- 使用ASP.NET Atlas编写显示真实进度的ProgressBar(进度条)控件
- Flex进度条控件ProgressBar使用讲解加载图片显示进度演示
- 使用ASP.NET Atlas编写显示真实进度的ProgressBar(进度条)控件
- Android使用ProgressBar、自定义Notification显示文件下载进度
- 【进度显示】progressbar进度条的使用
- 使用ASP.NETAtlas编写显示真实进度的ProgressBar(进度条)控件
- ProgressBar使用自定义drawable时,进度显示不全的问题
- 使用ASP.NET Atlas编写显示真实进度的ProgressBar(进度条)控件
- 使用ASP.NET Atlas编写显示真实进度的ProgressBar(进度条)控件
- 使用ASP.NET Atlas编写显示真实进度的ProgressBar(进度条)控件
- 使用ASP.NET Atlas编写显示真实进度的ProgressBar(进度条)控件
- 使用ASP.NET Atlas编写显示真实进度的ProgressBar(进度条)控件
- Android 使用ProgressBar实现进度条
- 使用libcurl显示下载进度
- 使用C#在进度条中显示复制文件的进度
- 使用DirectShow播放MP3,实现循环播放,显示播放进度
- 使用OKHttp3实现下载(断点续传、显示进度)
- Android 下载模块,使用Xutils3 的下载功能,Activity和services数据交互,listview显示下载进度
- 使用重写的$.ajax()实现后台处理进度显示(gif)