7.Android之评分条RatingBar和拖动条SeekBar学习

评分条RatingBar和拖动条SeekBar很常见,今天来学习下。

(1)RatingBar评分条

如图:

7.Android之评分条RatingBar和拖动条SeekBar学习

 <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.example.ratingbar.MainActivity" > <RatingBar
android:id="@+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="22dp"
android:layout_marginTop="25dp" /> </RelativeLayout>

RatingBar监听事件:public void setOnRatingBarChangeListener (RatingBar.OnRatingBarChangeListener listener)

(2)拖动他SeekBar使用

首先从工具中拖入一个SeekBar和两个TextView,如图:

7.Android之评分条RatingBar和拖动条SeekBar学习

代码如下:

 <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.example.ratingbar.MainActivity" > <SeekBar
android:id="@+id/seekBar1"
android:max="100"
android:progress="50"
android:secondaryProgress="75"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="35dp" /> <TextView
android:id="@+id/info1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/seekBar1"
android:layout_below="@+id/seekBar1"
android:layout_marginTop="24dp"/> <TextView
android:id="@+id/info2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/info1"
android:layout_below="@+id/info1"
android:layout_marginTop="32dp"/> </RelativeLayout>

然后我们实现OnSeekBarChangeListener接口:public class MainActivity extends Activity implements OnSeekBarChangeListener{...} ,此接口共需要监听三个事件,分别是:
                 数值改变(onProgressChanged
                 开始拖动(onStartTrackingTouch
                 停止拖动(onStopTrackingTouch

完整代码如下:

 package com.example.ratingbar;

 import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView; //实现OnSeekBarChangeListener接口
public class MainActivity extends Activity implements OnSeekBarChangeListener { private SeekBar sbVolumer=null;
private TextView info1=null;
private TextView info2=null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); sbVolumer = (SeekBar) findViewById(R.id.seekBar1);
sbVolumer.setOnSeekBarChangeListener(this);
info1 = (TextView) findViewById(R.id.info1);
info2 = (TextView) findViewById(R.id.info2);
} @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);
} @Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) { info1.setText("当前值: " + progress);
} @Override
public void onStartTrackingTouch(SeekBar seekBar) { info2.setText("正在改变当前值");
} @Override
public void onStopTrackingTouch(SeekBar seekBar) {
info2.setText("停止拖动");
}
}

运行效果:

7.Android之评分条RatingBar和拖动条SeekBar学习         7.Android之评分条RatingBar和拖动条SeekBar学习

上一篇:InnoDB为什么要使用auto_Increment


下一篇:linux下eclipse环境配置的坑