拖动条能够由用户自己进行手工的调节,比如:当用户须要调整播放器音量或者是电影的播放进度时都会使用到拖动条,SeekBar类的定义结构例如以下所看到的:
java.lang.Object
↳ android.view.View
↳ android.widget.ProgressBar
↳ android.widget.AbsSeekBar
↳ android.widget.SeekBar
经常用法
|
public SeekBar(Context context)
|
构造
|
创建SeekBar类的对象
|
|
public void setOnSeekBarChangeListener(
SeekBar.OnSeekBarChangeListener l)
|
普通
|
设置改变监听操作
|
|
public synchronized void setMax(int max)
|
普通
|
设置增长的最大值
|
public static
interface SeekBar.OnSeekBarChangeListener{
/**
* 開始拖动时触发操作
* @param seekBar
触发操作的SeekBar组件对象
*/
public
abstract
void onStartTrackingTouch(SeekBar seekBar) ;
/**
* @param seekBar
触发操作的SeekBar组件对象
* @param progress
当前的进度值
* @param fromUser
是否为用户自己触发
*/
public
abstract
void onProgressChanged(SeekBar seekBar,
int progress, boolean fromUser) ;
/**
* 停止拖动时触发操作
* @param seekBar
触发操作的SeekBar组件对象
*/
public
abstract
void onStopTrackingTouch(SeekBar seekBar) ;
}
主要的使用
xml文件
<span style="font-size:18px;"><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=".MainActivity" >
<SeekBar
android:max="100"
android:progress="30"
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="60dp" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/seekBar1"
android:layout_below="@+id/seekBar1"
android:layout_marginLeft="28dp"
android:layout_marginTop="32dp"
android:text="seek1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<SeekBar
android:id="@+id/seekBar2"
android:max="100"
android:secondaryProgress="60"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="67dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/seekBar2"
android:layout_marginTop="28dp"
android:text="seek2"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
</span>java文件
<span style="font-size:18px;">package com.example.seekbar;
import android.app.Activity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
public class MainActivity extends Activity implements OnSeekBarChangeListener{
private TextView textView1,textView2;
private SeekBar seekBar1,seekBar2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1=(TextView)this.findViewById(R.id.textView1);
textView2=(TextView)this.findViewById(R.id.textView2);
seekBar1=(SeekBar)this.findViewById(R.id.seekBar1);
seekBar2=(SeekBar)this.findViewById(R.id.seekBar2);
seekBar1.setOnSeekBarChangeListener(this);
seekBar2.setOnSeekBarChangeListener(this);
}
@Override
public void onProgressChanged(SeekBar seekBar, int position, boolean flag) {
// TODO Auto-generated method stub
if (seekBar.getId()==seekBar1.getId())
{
textView1.setText("当前seekbar1刻度"+position);
}
else {
textView2.setText("当前seekbar2刻度"+position);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
if (seekBar.getId()==seekBar1.getId())
{
textView1.setText("開始滑动seek1");
}
else {
textView2.setText("開始滑动seek2");
}
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
if (seekBar.getId()==seekBar1.getId())
{
textView1.setText("停止滑动seek1");
}
else {
textView2.setText("停止滑动seek2");
}
}
}
</span>效果图
使用seekbar来控制屏幕的亮度
xml文件
<span style="font-size:18px;"><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=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"
android:text="调节手机亮度" />
<SeekBar
android:max="100"
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="117dp"
android:progress="50" />
</RelativeLayout></span>JAVA文件
<span style="font-size:18px;">package com.example.seekbardemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class MainActivity extends Activity implements OnSeekBarChangeListener {
private SeekBar myseekBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myseekBar=(SeekBar)this.findViewById(R.id.seekBar1);
myseekBar.setOnSeekBarChangeListener(this);
}
@Override
public void onProgressChanged(SeekBar seekBar, int position, boolean flag) {
// TODO Auto-generated method stub
}
//调节亮度的方法
private void setScreenBrightness(float num) {
WindowManager.LayoutParams layoutParams =
getWindow().getAttributes(); // 取得window属性
layoutParams.screenBrightness = num; // num已经除以100
super.getWindow().setAttributes(layoutParams); // 0~1之间
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
//在拖动结束是使用getProgress获得当前的Progress值来设置亮度
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
if (seekBar.getId()==myseekBar.getId()) {
// 将progress除以100并转换为float类型
setScreenBrightness((float)seekBar.getProgress()/100);
}
}
}
</span>效果图
改变后
当然,使用SeekBar组件也能够对音量进行控制,大家能够查询相关API自行尝试
下节预报:RatingBar评分组件