3.6bar控件
ProcessBar:进度条
SeekBar:拖动条
RatingBar:评分控件
界面:
布局:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.liang.barliang.MainActivity"> <ProgressBar style="?android:attr/progressBarStyleHorizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/progressBar" android:layout_alignParentTop="true" android:layout_alignParentStart="true" android:layout_alignParentEnd="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="download" android:id="@+id/button" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" /> <SeekBar android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/seekBar" android:layout_below="@+id/progressBar" android:layout_alignParentStart="true" android:layout_marginTop="52dp" android:layout_alignEnd="@+id/progressBar" /> <RatingBar android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/ratingBar" android:layout_marginTop="52dp" android:layout_below="@+id/seekBar" android:layout_centerHorizontal="true" /> </RelativeLayout>
程序:
import android.content.Intent; import android.os.AsyncTask; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ProgressBar; import android.widget.RatingBar; import android.widget.SeekBar; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private final static String TAG = "bar"; private Button button; private ProgressBar progressBar; private SeekBar seekBar; private RatingBar ratingBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) this.findViewById(R.id.button); progressBar = (ProgressBar) this.findViewById(R.id.progressBar); seekBar = (SeekBar) this.findViewById(R.id.seekBar); ratingBar = (RatingBar) this.findViewById(R.id.ratingBar); progressBar.setMax(100); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new DownloadTask().execute(); } }); // seekBar.setMax(100); seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { //实时变化的回调函数 } @Override public void onStartTrackingTouch(SeekBar seekBar) { //开始滚动的回调函数 } @Override public void onStopTrackingTouch(SeekBar seekBar) { //滚动结束的回调函数 Toast.makeText(MainActivity.this, "Volume:" + seekBar.getProgress(), Toast.LENGTH_SHORT).show(); } }); //评分控件 ratingBar.setNumStars(5); ratingBar.setMax(100); ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() { @Override public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) { Toast.makeText(MainActivity.this, "Stars:" + rating, Toast.LENGTH_SHORT).show(); } }); } //进度控件 下载实时更新 class DownloadTask extends AsyncTask<Void, Integer, Void> { @Override protected void onProgressUpdate(Integer... values) { super.onProgressUpdate(values); progressBar.setProgress(values[0]); } @Override protected Void doInBackground(Void... params) { int i = 1; while (i <= 100) { try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } Log.i(TAG, String.valueOf(i)); publishProgress(i); i++; } return null; } } }