评分条RatingBar和拖动条SeekBar很常见,今天来学习下。
(1)RatingBar评分条
如图:
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context="com.example.ratingbar.MainActivity" > 10 11 <RatingBar 12 android:id="@+id/ratingBar1" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:layout_alignParentLeft="true" 16 android:layout_alignParentTop="true" 17 android:layout_marginLeft="22dp" 18 android:layout_marginTop="25dp" /> 19 20 </RelativeLayout>
RatingBar监听事件:public void setOnRatingBarChangeListener (RatingBar.OnRatingBarChangeListener listener)
(2)拖动他SeekBar使用
首先从工具中拖入一个SeekBar和两个TextView,如图:
代码如下:
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context="com.example.ratingbar.MainActivity" > 10 11 <SeekBar 12 android:id="@+id/seekBar1" 13 android:max="100" 14 android:progress="50" 15 android:secondaryProgress="75" 16 android:layout_width="match_parent" 17 android:layout_height="wrap_content" 18 android:layout_alignParentLeft="true" 19 android:layout_alignParentTop="true" 20 android:layout_marginTop="35dp" /> 21 22 <TextView 23 android:id="@+id/info1" 24 android:layout_width="wrap_content" 25 android:layout_height="wrap_content" 26 android:layout_alignLeft="@+id/seekBar1" 27 android:layout_below="@+id/seekBar1" 28 android:layout_marginTop="24dp"/> 29 30 <TextView 31 android:id="@+id/info2" 32 android:layout_width="wrap_content" 33 android:layout_height="wrap_content" 34 android:layout_alignLeft="@+id/info1" 35 android:layout_below="@+id/info1" 36 android:layout_marginTop="32dp"/> 37 38 </RelativeLayout>
然后我们实现OnSeekBarChangeListener接口:public class MainActivity extends Activity implements OnSeekBarChangeListener{...} ,此接口共需要监听三个事件,分别是:
数值改变(onProgressChanged)
开始拖动(onStartTrackingTouch)
停止拖动(onStopTrackingTouch)
完整代码如下:
1 package com.example.ratingbar; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.Menu; 6 import android.view.MenuItem; 7 import android.widget.SeekBar; 8 import android.widget.SeekBar.OnSeekBarChangeListener; 9 import android.widget.TextView; 10 11 //实现OnSeekBarChangeListener接口 12 public class MainActivity extends Activity implements OnSeekBarChangeListener { 13 14 private SeekBar sbVolumer=null; 15 private TextView info1=null; 16 private TextView info2=null; 17 18 @Override 19 protected void onCreate(Bundle savedInstanceState) { 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.activity_main); 22 23 sbVolumer = (SeekBar) findViewById(R.id.seekBar1); 24 sbVolumer.setOnSeekBarChangeListener(this); 25 info1 = (TextView) findViewById(R.id.info1); 26 info2 = (TextView) findViewById(R.id.info2); 27 } 28 29 @Override 30 public boolean onCreateOptionsMenu(Menu menu) { 31 // Inflate the menu; this adds items to the action bar if it is present. 32 getMenuInflater().inflate(R.menu.main, menu); 33 return true; 34 } 35 36 @Override 37 public boolean onOptionsItemSelected(MenuItem item) { 38 // Handle action bar item clicks here. The action bar will 39 // automatically handle clicks on the Home/Up button, so long 40 // as you specify a parent activity in AndroidManifest.xml. 41 int id = item.getItemId(); 42 if (id == R.id.action_settings) { 43 return true; 44 } 45 return super.onOptionsItemSelected(item); 46 } 47 48 @Override 49 public void onProgressChanged(SeekBar seekBar, int progress, 50 boolean fromUser) { 51 52 info1.setText("当前值: " + progress); 53 } 54 55 @Override 56 public void onStartTrackingTouch(SeekBar seekBar) { 57 58 info2.setText("正在改变当前值"); 59 } 60 61 @Override 62 public void onStopTrackingTouch(SeekBar seekBar) { 63 info2.setText("停止拖动"); 64 } 65 }
运行效果: