rAndroid(11):进度条ProgressBar/SeekBar/RatingBar

来源:互联网 发布:sql语句拼接 单双引号 编辑:程序博客网 时间:2024/04/29 01:42
  1. Android中的进度条:
    这里写图片描述
  2. 第一排的是ProgressBar,他们风格不同而已,第二排的三和四是ProgressBar的子类
    这里写图片描述
  3. 在xml文件中设置ProgressBar的属性
<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="horizontal" ><ProgressBar     android:id="@+id/progressBar"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:max="200"    android:progress="100"    android:secondaryProgress="150"    style="?android:attr/progressBarStyleHorizontal"    /><Button    android:id="@+id/firstProgress_btn"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="增加第一进度"></Button><Button    android:id="@+id/secondProgress_btn"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="增加第二进度"></Button></LinearLayout>
  1. 在代码中设置ProgressBar的属性
package com.borqs.firstpj;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.DatePicker;import android.widget.ProgressBar;import android.widget.TimePicker;import android.widget.TimePicker.OnTimeChangedListener;public class MainActivity extends ActionBarActivity {    ProgressBar mProgressBar;    Button mFirstProgress;    Button mSecondProgress;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mProgressBar = (ProgressBar)findViewById(R.id.progressBar);        mFirstProgress = (Button)findViewById(R.id.firstProgress_btn);        mSecondProgress = (Button)findViewById(R.id.secondProgress_btn);        //设置进度条的最大值//      mProgressBar.setMax(300);        //设置进度条的当前进度        mProgressBar.setProgress(100);        //设置进度条的第二进度值        mProgressBar.setSecondaryProgress(150);        //是否是明确的方法,如果ProgressBar是明确的返回false,如果ProgressBar是不明确的返回ture        boolean flag = mProgressBar.isIndeterminate();        ProgressButtonListener listener = new ProgressButtonListener();        mFirstProgress.setOnClickListener(listener);        SecondProgressButtonListener l = new SecondProgressButtonListener();        mSecondProgress.setOnClickListener(l);    }    class ProgressButtonListener implements OnClickListener{        @Override        public void onClick(View v) {            mProgressBar.incrementProgressBy(10);        }    }    class SecondProgressButtonListener implements OnClickListener{        @Override        public void onClick(View v) {            mProgressBar.incrementSecondaryProgressBy(20);        }    }}

这里写图片描述


SeekBar

<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= "horizontal">    <SeekBar         android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/seekBar"/></LinearLayout>
public class MainActivity extends ActionBarActivity {    private SeekBar mSeekBar;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mSeekBar = (SeekBar)findViewById(R.id.seekBar);        mSeekBar.setProgress(10);        mSeekBar.setSecondaryProgress(50);        onChangeListener listener = new onChangeListener();        mSeekBar.setOnSeekBarChangeListener(listener);    }    class onChangeListener implements OnSeekBarChangeListener{        /**         * seekBar:指的是触发了监听器的Seekbar对象         * progress:当前进度         * fromUser:这次的变化是不是有用户引起的,如果是用户拖拽导致的SeekBar的变化是true         */        @Override        public void onProgressChanged(SeekBar seekBar, int progress,                boolean fromUser) {            System.out.println("seekBar:" + seekBar + ",progress:" + progress + ",fromUser:" + fromUser);        }        @Override        public void onStartTrackingTouch(SeekBar seekBar) {            System.out.println("start");        }        @Override        public void onStopTrackingTouch(SeekBar seekBar) {            System.out.println("stop");        }    }

RatingBar

<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= "horizontal">    <RatingBar         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/ratingBar"/>    <Button        android:id="@+id/btn"        android:text="button"        android:layout_width="match_parent"        android:layout_height="wrap_content"/></LinearLayout>
    private RatingBar mRatingBar;    private Button mButton;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mRatingBar = (RatingBar)findViewById(R.id.ratingBar);        mButton = (Button)findViewById(R.id.btn);        //总共5颗星        mRatingBar.setNumStars(5);        //一次前进一颗星        mRatingBar.setStepSize(1);        //监听步进变化        RatingBarChangedListener listener = new RatingBarChangedListener();        mRatingBar.setOnRatingBarChangeListener(listener);        //测试button改变ratingbar步进值,fromUser为false.        buttonClickChangedListener l = new buttonClickChangedListener();        mButton.setOnClickListener(l);    }    class RatingBarChangedListener implements OnRatingBarChangeListener{        @Override        public void onRatingChanged(RatingBar ratingBar, float rating,                boolean fromUser) {            System.out.println("rating:" + rating + ",fromUser:" + fromUser);        }    }    class buttonClickChangedListener implements OnClickListener{        @Override        public void onClick(View v) {            mRatingBar.setRating(mRatingBar.getRating() + 2.0f);        }    }

结果:当使用button改变ratingBar步进值时fromUser为false.

0 0