您的位置:首页 > 其它

使用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"                    //最大刻度
<?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次 效果如下



点击重置按钮如下

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: