ProgressBar的使用以及实现进度条的增减

ProgressBar 的使用,以及通过按钮实现增加和减少第一进度和第二进度条。 
重要方法提要 
设置窗口特征: requestWindowFeature(Window.FEATURE_PROGRESS); 
获取进度条: 
int first=progressBar.getProgress(); 
int second=progressBar.getSecondaryProgress(); 
int max=progressBar.getMax(); 
改变进度条的大小 
progressBar.incrementProgressBy(int); 

1. 布局 

<LinearLayout 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:orientation="vertical"
    tools:context="com.example.progressbar.MainActivity" >




    <ProgressBar
        android:progress="50"
        android:secondaryProgress="80"
        android:max="100"
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
/>


    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="增加" />


    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="减少" />


    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="重置" />


    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />


</LinearLayout>


2.java 代码


package com.example.progressbar;


import android.R.integer;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;


public class MainActivity extends Activity implements android.view.View.OnClickListener{
private Button button1;
private Button button2;
private Button button3;
private ProgressBar  progressBar;
private TextView textview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置窗口特征
requestWindowFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.activity_main);
setProgressBarIndeterminateVisibility(true);
progressBar=(ProgressBar) findViewById(R.id.progressBar1);
button1=(Button) findViewById(R.id.button1);
button2=(Button) findViewById(R.id.button2);
button3=(Button) findViewById(R.id.button3);
textview=(TextView) findViewById(R.id.textView1);
//获取第一进度,第二进度,最大进度
int first=progressBar.getProgress();
int second=progressBar.getSecondaryProgress();
int max=progressBar.getMax();
//将获取的显示出来
textview.setText("第一进度"+first+"  第二进度"+second+"  第一进度占的百分比"+
(first/(float)max)*100+"%");
//按钮设置监听事件
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);

}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
//增加进度条的刻度
case R.id.button1:
progressBar.incrementProgressBy(10);
progressBar.incrementSecondaryProgressBy(10);
break;
//减少进度条的刻度
case R.id.button2:
progressBar.incrementProgressBy(-10);
progressBar.incrementSecondaryProgressBy(-10);
break;
case R.id.button3:
progressBar.setProgress(50);
progressBar.setSecondaryProgress(80);
break;
}
textview.setText("第一进度"+progressBar.getProgress()+" 第二进度"+progressBar.getSecondaryProgress()+" 第一进度占的百分比"
+(progressBar.getProgress()/(float)progressBar.getMax())*100+"%");

}



}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AnjoyZhang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值