android---拖动条(SeekBar)

来源:互联网 发布:php手册中文版手机版 编辑:程序博客网 时间:2024/05/19 16:33

拖动条可以被用户控制,所以需要对其进行事件监听,需要实现SeekBar.OnSeekBarChangeListener接口。在SeekBar中需要实现监听3个事件:

数值改变:onProgressChanged

开始拖动:onStartTrackingTouch

停止拖动:onStopTrackingTouch

main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    ><SeekBar android:layout_height="wrap_content" android:id="@+id/seekBar1"android:layout_width="fill_parent"android:max="100"></SeekBar><TextView      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="@string/hello"    android:id="@+id/text1"    />    <TextView      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="@string/hello"    android:id="@+id/text2"    /></LinearLayout>


seekBArActivity.java

package com.seekBar;import android.app.Activity;import android.os.Bundle;import android.widget.SeekBar;import android.widget.TextView;public class seekBArActivity extends Activity implements SeekBar.OnSeekBarChangeListener{    SeekBar mysSeekBar;    TextView textView1,textView2;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        mysSeekBar=(SeekBar) findViewById(R.id.seekBar1);        textView1=(TextView) findViewById(R.id.text1);        textView2=(TextView) findViewById(R.id.text2);        mysSeekBar.setOnSeekBarChangeListener(this);            }@Override//监听数值改变事件public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {textView1.setText("当前值:"+progress);}@Override//监听开始拖动事件public void onStartTrackingTouch(SeekBar seekBar) {// TODO Auto-generated method stubtextView2.setText("正在调节");}@Override//监听停止拖动事件public void onStopTrackingTouch(SeekBar seekBar) {// TODO Auto-generated method stubtextView2.setText("调节结束");}}