UI组件之ProgressBar及其子类(二)SeekBar拖动条和RatingBar星级评分条的使用

最后更新于:2022-04-01 16:14:18

拖动条采用拖动滑块的位置来表示数值 SeekBar的常用xml属性值: ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-03-10_56e0d9adab0c3.jpg) **重要的android:thumb制定一个Drawable对象,改变滑块外观** 通过滑块来改变图片的透明度: main.xml ~~~ <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/root" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="300dp" android:src="@drawable/lijiang" /> <!-- android:thumb 是滑块的图片, android:progressDrawable是滑条的图片 --> <SeekBar android:id="@+id/seekBar1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:max="255" android:progress="255" android:thumb="@drawable/ic_launcher" /> <TextView android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="25dp" /> <TextView android:id="@+id/textView2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="25dp" /> </LinearLayout> ~~~ MainActivity.java ~~~ package com.example.seekbartest; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.ImageView; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.TextView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final ImageView image =(ImageView) findViewById(R.id.imageView1); SeekBar seekbar=(SeekBar) findViewById(R.id.seekBar1); final TextView textView1 = (TextView) this.findViewById(R.id.textView1); final TextView textView2 = (TextView) this.findViewById(R.id.textView2); //设置拖动条的状态改变监听器 seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){ //当拖动条的滑块位置发生改变时触发该方法 @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // TODO Auto-generated method stub image.setAlpha(progress);//设置图片透明度 textView1.setText("当前值:"+progress); } @Override public void onStartTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub textView2.setText("拖动中..."); } @Override public void onStopTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub textView2.setText("拖动完毕"); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } } ~~~ ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-03-10_56e0d9adc1224.jpg) 自定义拖动条比较好,留着以后借鉴:[http://blog.csdn.net/imdxt1986/article/details/7609164](http://blog.csdn.net/imdxt1986/article/details/7609164) 星级评分条的常用xml属性: ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-03-10_56e0d9adedfcb.jpg) android:isIndicator:设置该评分条是否允许用户改变(true为不允许修改) android:numStars:设置总共有多少个星级 android:rating设置星级评分条默认的星级 android:stepSize设置没吃最少需要改变多少个星级,0.5半个星星,1就是1个星星 使用星级评分改变图片透明度: main.xml ~~~ <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/root" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="300dp" android:src="@drawable/lijiang" /> <RatingBar android:id="@+id/ratingBar1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:max="255" android:numStars="6" android:progress="255" android:stepSize="0.5" /> </LinearLayout> ~~~ MainActivity.java ~~~ package com.example.ratingbartest; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.ImageView; import android.widget.RatingBar; import android.widget.RatingBar.OnRatingBarChangeListener; import android.widget.SeekBar; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final ImageView image =(ImageView) findViewById(R.id.imageView1); RatingBar bar=(RatingBar) findViewById(R.id.ratingBar1); //设置星级评论条状态改变监听器 bar.setOnRatingBarChangeListener(new OnRatingBarChangeListener(){ @Override public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) { // TODO Auto-generated method stub image.setAlpha((int)(rating*255/6));//设置图片透明度 } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } } ~~~ ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-03-10_56e0d9ae10f14.jpg)
';