seekbar拖动条控件可以通过拖动条改变当前的值,可以用seebar设置具有一定范围的变量值。如:
在.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"> <TextView android:id="@+id/textview1" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/textview2" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <SeekBar android:id="@+id/seekbar1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:max="100" android:progress="30"></SeekBar> <SeekBar android:id="@+id/seekbar2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:max="100" android:progress="30" android:secondaryProgress="60"></SeekBar> </LinearLayout>
在.java文件中:
public class Main extends Activity implements OnSeekBarChangeListener { /** Called when the activity is first created. */ private TextView textView1, textView2; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); textView1 = (TextView) this.findViewById(R.id.textview1); textView2 = (TextView) this.findViewById(R.id.textview2); SeekBar seekBar1 = (SeekBar) this.findViewById(R.id.seekbar1); SeekBar seekBar2 = (SeekBar) this.findViewById(R.id.seekbar2); seekBar1.setOnSeekBarChangeListener(this); seekBar2.setOnSeekBarChangeListener(this); } // 当滑动滑竿的时候会触发的事件 @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // TODO Auto-generated method stub if (seekBar.getId() == R.id.seekbar1) { textView1.setText("seekbar1的当前位置是:" + progress); } else { textView2.setText("seekbar2的当前位置是:" + progress); } } // 表示从哪里开始拖动 @Override public void onStartTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub if (seekBar.getId() == R.id.seekbar1) { textView1.setText("seekbar1开始拖动"); } else { textView1.setText("seekbar2开始拖动"); } } // 表示从哪里结束拖动 @Override public void onStopTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub if (seekBar.getId() == R.id.seekbar1) { textView1.setText("seekbar1停止拖动"); } else { textView1.setText("seekbar2停止拖动"); } } }
运行结果: