seekbar的使用(数值拖动框的使用—用于选择数值)

来源:互联网 发布:dos是什么办公软件 编辑:程序博客网 时间:2024/06/01 22:52

用处:可用于视频播放


布局文件:

<?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>



代码:

添加监听事件:onseekbarlistener  实现监听:

监听函数:第一个参数表示:bar对象,第二个表示参数。

表示从哪里开始拖动:onstarttrackingtouch()

从哪里结束拖动:onstoptrackingtouch()

package com.example.marvinedittext2;import android.app.Activity;import android.os.Bundle;import android.widget.SeekBar;import android.widget.TextView;import android.widget.SeekBar.OnSeekBarChangeListener;public class Main extends Activity implements OnSeekBarChangeListener {/** Called when the activity is first created. */private TextView textView1, textView2;@Overridepublic 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);}// 当滑动滑竿的时候会触发的事件@Overridepublic void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {// TODO Auto-generated method stubif (seekBar.getId() == R.id.seekbar1) {textView1.setText("seekbar1的当前位置是:" + progress);} else {textView2.setText("seekbar2的当前位置是:" + progress);}}// 表示从哪里开始拖动@Overridepublic void onStartTrackingTouch(SeekBar seekBar) {// TODO Auto-generated method stubif (seekBar.getId() == R.id.seekbar1) {textView1.setText("seekbar1开始拖动");} else {textView1.setText("seekbar2开始拖动");}}// 表示从哪里结束拖动@Overridepublic void onStopTrackingTouch(SeekBar seekBar) {// TODO Auto-generated method stubif (seekBar.getId() == R.id.seekbar1) {textView1.setText("seekbar1停止拖动");} else {textView1.setText("seekbar2停止拖动");}}}


总结:还是用监听,进行相应的操作。


拓展:判断多个组件用用一个监听的时候,判断到底是哪一个组件,方法是用id进行判断的。在上面的代码有例子。


0 0
原创粉丝点击