ProgressBar进度条的实现

各式各样的进度条,这里不演示效果了,大家可以自己写写,动手操作操作,看看效果

MainActivty代码:

package com.example.sixth_progressbar_android;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
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 Activity implements View.OnClickListener{
    private ProgressBar press;
    private Button add;
    private Button reduce;
    private Button reset;
    private Button show;
    private ProgressDialog progressdialog;
    private TextView text2;

    @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(true);

        setProgress(5000);

        intt();

    }

    private void intt() {
        press = (ProgressBar)findViewById(R.id.horiz);
        add = (Button) findViewById(R.id.add);
        reduce = (Button)findViewById(R.id.reduce);
        reset = (Button) findViewById(R.id.reset);
        text2 = (TextView)findViewById(R.id.text1);
        show = (Button)findViewById(R.id.show);

        //获取第一进度
        int first = press.getProgress();

        //获得第二进度
        int second = press.getSecondaryProgress();

        //获取最大进度
        int max = press.getMax();

        text2.setText("第一进度:"+(int)(first/(float)max*100) +"%  第二进度:"+(int)(second/(float)max*100) +"%");

        //设置监听器
        add.setOnClickListener(this);
        reset.setOnClickListener(this);
        reduce.setOnClickListener(this);
        show.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        switch(view.getId())
        {
            case R.id.add: {
                //增加第一进度和第二进度10%
                press.incrementProgressBy(10);
                press.incrementSecondaryProgressBy(10);

                break;
            }
            case R.id.reduce: {
                //减少第一进度和第二进度10%
                press.incrementProgressBy(-20);
                press.incrementSecondaryProgressBy(-20);

                break;
            }
            case R.id.reset: {
                //重置
                press.setProgress(50);
                press.setSecondaryProgress(80);

                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一些属性设置
                  */
                 //设定最大进度
                 progressdialog.setMax(100);
                 //初始化进度
                 progressdialog.incrementProgressBy(50);
                 //进度条是明确清晰可见
                 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
                 progressdialog.show();

                 break;
             }

        }

        text2.setText("第一进度:"+(int)(press.getProgress()/(float)press.getMax()*100)+"% " +
                      " 第二进度:"+(int)(press.getSecondaryProgress()/(float)press.getMax()*100)+"%");
    }
}
xml文件代码:





    

    

    

    

    



你可能感兴趣的:(Android)