Android程序:进度条(ProgressBar)的使用方法和案例

来源:互联网 发布:php空间服务器 编辑:程序博客网 时间:2024/06/06 01:10

基本用法:
这里写图片描述
这里写图片描述
MainActivity :

public class MainActivity extends Activity implements OnClickListener {    private ProgressBar progressBar;    private Button btAdd;    private Button btReduce;    private Button btReSet;    private TextView text;    private ProgressDialog progressDialog;    private Button btShow;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        progressBar = (ProgressBar) findViewById(R.id.progressBar);        btAdd = (Button) findViewById(R.id.add);        btReduce = (Button) findViewById(R.id.reduce);        btReSet = (Button) findViewById(R.id.reset);        text = (TextView) findViewById(R.id.text);        btShow = (Button) findViewById(R.id.show);        // 获取第一条进度条的进度        int first = progressBar.getProgress();        // 获取第二条进度条的进度        int second = progressBar.getSecondaryProgress();        // 获取进度条的最大进度        int max = progressBar.getMax();        text.setText("第一进度百分比:" + (int) (first * 100.0 / max) + "%,第二进度百分比:"                + (int) (second * 100.0 / max) + "%");        btAdd.setOnClickListener(this);        btReduce.setOnClickListener(this);        btReSet.setOnClickListener(this);        btShow.setOnClickListener(this);    }    @Override    public void onClick(View v) {        switch (v.getId()) {        case R.id.add:            // 两个进度都增加10个刻度            progressBar.incrementProgressBy(10);            progressBar.incrementSecondaryProgressBy(10);            break;        case R.id.reduce:            progressBar.incrementProgressBy(-10);            progressBar.incrementSecondaryProgressBy(-10);            break;        case R.id.reset:            progressBar.setProgress(50);            progressBar.setSecondaryProgress(80);            break;        case R.id.show:            //新建ProgressDialog对象            progressDialog=new ProgressDialog(this);            //设置显示风格            progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);            //设置标题            progressDialog.setTitle("对话框进度条");            //设置对话框中的文字            progressDialog.setMessage("这是对话框进度条");            //设置图标            progressDialog.setIcon(R.drawable.ic_launcher);            /**             * 设定关于ProgressBar的一些属性             */            //设定最大进度            progressDialog.setMax(100);            //设定初始化的进度            progressDialog.incrementProgressBy(50);            //进度条是明确显示进度的            progressDialog.setIndeterminate(false);            /**             * 设定一个确定按钮             */            progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {                @Override                public void onClick(DialogInterface dialog, int which) {                    Toast.makeText(MainActivity.this, "点了确定", Toast.LENGTH_SHORT).show();                }            });            //是否可以通过返回按钮退出对话框            progressDialog.setCancelable(true);            //显示ProgressDialog            progressDialog.show();            break;        }        text.setText("第一进度百分比:"                + (int) (progressBar.getProgress() * 100.0 / progressBar                        .getMax())                + "%,第二进度百分比:"                + (int) (progressBar.getSecondaryProgress() * 100.0 / progressBar                        .getMax()) + "%");    }}

main.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <!--    android:progressDrawable="" 可以定义配色方案    -->    <ProgressBar        android:id="@+id/progressBar"        style="?android:attr/progressBarStyleHorizontal"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:max="100"        android:progress="50"        android:secondaryProgress="80" />    <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" />    <Button        android:id="@+id/show"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/show" /></LinearLayout>
0 0
原创粉丝点击