安卓——ProgressBar







通过按钮增减或减少第一进度或第二进度的值,会在一个TextView显示两种进度的百分比。点击重置时百分比为0。







activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.lenovo.progressbar.MainActivity">

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        />

    <ProgressBar
        android:id="@+id/horiz"
        android:max="100"
        android:progress="50"
        android:secondaryProgress="80"
    style="@android:style/Widget.ProgressBar.Horizontal"
        android:progressDrawable="@drawable/progress_bar"
    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="@string/add" />

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

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

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

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

</LinearLayout>

MainActivity.java

package com.example.lenovo.progressbar;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements View.OnClickListener{

    private ProgressBar progress;
    private Button add;
    private Button reduce;
    private Button reset;
    private Button show;
    private TextView textView;
    private ProgressDialog progressDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();
    }

    private void init() {
        progress=(ProgressBar)findViewById(R.id.horiz);
        add=(Button)findViewById(R.id.add);
        reduce=(Button)findViewById(R.id.reduce);
        reset=(Button)findViewById(R.id.reset);
        show=(Button)findViewById(R.id.show);
        textView=(TextView)findViewById(R.id.text);

        //获取第一进度的值
        int first=progress.getProgress();
        //获取第二进度的值
        int second=progress.getSecondaryProgress();
        //获取最大值
        int max=progress.getMax();
        //设值刚开始时textView中的内容
        textView.setText("第一进度百分比: "+(int)(first/(float)max*100)+"% 第二进度的百分比: "+(int)(second/(float)max*100)+"%");
        //设置监听器
        add.setOnClickListener(this);
        reduce.setOnClickListener(this);
        reset.setOnClickListener(this);
        show.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {

        switch(view.getId())
        {
            case R.id.add:
                //第一二进度各增加10个单位
                progress.incrementProgressBy(10);
                progress.incrementSecondaryProgressBy(10);
                break;
            case R.id.reduce:
                //第一二进度各减少10个单位
                progress.incrementProgressBy(-10);
                progress.incrementSecondaryProgressBy(-10);
                break;
            case R.id.reset:
                //重置
                progress.setProgress(50);
                progress.setSecondaryProgress(80);
                break;
            case R.id.show:
                /*
                页面显示风格
                 */
                //新建ProgressDialog对象
                progressDialog =new ProgressDialog(MainActivity.this);
                //设置ProgressDialog风格
                progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                //设置标题
                progressDialog.setTitle("慕课网");
                //设置对话框里面的文字信息
                progressDialog.setMessage("欢迎大家支持慕课网");
                //设置图标
                progressDialog.setIcon(R.mipmap.ic_launcher);

                /*
                设定关于ProgressBar的一些属性
                 */
                //设置最大进度
                progressDialog.setMax(100);
                //设置初始化已经增加大的进度
               progressDialog.incrementProgressBy(50);
                //进度条是明确显示进度的  注意:必须填写false才能显示进度条
                progressDialog.setIndeterminate(false);

                /*
                设置一个确定按钮
                 */
                progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this,"欢迎大家支持慕课网",Toast.LENGTH_SHORT).show();
                    }
                });

                //是否可以通过返回按钮退出对话框
                progressDialog.setCancelable(true);

                //显示对话框
                progressDialog.show();
                break;
            default:
                break;
        }
        textView.setText("第一进度百分比: "+(int)(progress.getProgress()/(float)progress.getMax()*100)+"% 第二进度的百分比: "+(int)(progress.getSecondaryProgress()/(float)progress.getMax()*100)+"%");

    }
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

水之积也不厚,则其负大舟也无力

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

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

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

打赏作者

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

抵扣说明:

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

余额充值