Android控件之SeekBar

<p>1 SeekBar简介</p><p>SeekBar是进度条。我们使用进度条时,可以使用系统默认的进度条;也可以自定义进度条的图片和滑块图片等。</p><p>2 SeekBar示例</p><p>创建一个activity,包含2个SeekBar。</p><p>第1个SeekBar是系统默认的SeekBar。</p><p>第2个SeekBar是自定义SeekBar,使用自定义的背景图和滑块图片。</p><p>应用层代码</p><p>package com.skywang.control;</p><p>import android.os.Bundle;</p><p>import android.app.Activity;</p><p>import android.util.Log;</p><p>import android.widget.TextView;</p><p>import android.widget.SeekBar;</p><p>import android.widget.SeekBar.OnSeekBarChangeListener;</p><p>public class SeekBarTest extends Activity implements SeekBar.OnSeekBarChangeListener{</p><p> private static final String TAG = &quot;SKYWANG&quot;;</p><p> // 与&quot;系统默认SeekBar&quot;对应的TextView</p><p> private TextView mTvDef;</p><p> // 与&quot;自定义SeekBar&quot;对应的TextView</p><p> private TextView mTvSelf;</p><p> // &quot;系统默认SeekBar&quot;</p><p> private SeekBar mSeekBarDef;</p><p> // &quot;自定义SeekBar&quot;</p><p> private SeekBar mSeekBarSelf;</p><p> </p><p> @Override</p><p> protected void onCreate(Bundle savedInstanceState) {</p><p> super.onCreate(savedInstanceState);</p><p> setContentView(R.layout.seek_bar_test);</p><p> </p><p> // 与&quot;系统默认SeekBar&quot;对应的TextView</p><p> mTvDef = (TextView) findViewById(R.id.tv_def);</p><p> // &quot;系统默认SeekBar&quot;</p><p> mSeekBarDef = (SeekBar) findViewById(R.id.seekbar_def);</p><p> mSeekBarDef.setOnSeekBarChangeListener(this);</p><p> // 与&quot;自定义SeekBar&quot;对应的TextView</p><p> mTvSelf = (TextView) findViewById(R.id.tv_self);</p><p> // &quot;自定义SeekBar&quot;</p><p> mSeekBarSelf = (SeekBar) findViewById(R.id.seekbar_self);</p><p> mSeekBarSelf.setOnSeekBarChangeListener(this);</p><p> } </p><p> </p><p> /*</p><p> * SeekBar停止滚动的回调函数</p><p> */</p><p> @Override</p><p> public void onStopTrackingTouch(SeekBar seekBar) {</p><p> </p><p> }</p><p> /*</p><p> * SeekBar开始滚动的回调函数</p><p> */</p><p> @Override</p><p> public void onStartTrackingTouch(SeekBar seekBar) {</p><p> }</p><p> /*</p><p> * SeekBar滚动时的回调函数</p><p> */</p><p> @Override</p><p> public void onProgressChanged(SeekBar seekBar, int progress,</p><p> boolean fromUser) {</p><p> Log.d(TAG, &quot;seekid:&quot;+seekBar.getId()+&quot;, progess&quot;+progress);</p><p> switch(seekBar.getId()) {</p><p> case R.id.seekbar_def:{</p><p> // 设置&quot;与系统默认SeekBar对应的TextView&quot;的值</p><p> mTvDef.setText(getResources().getString(R.string.text_def)+&quot; : &quot;+String.valueOf(seekBar.getProgress()));</p><p> break;</p><p> }</p><p> case R.id.seekbar_self: {</p><p> // 设置&quot;与自定义SeekBar对应的TextView&quot;的值 </p><p> mTvSelf.setText(getResources().getString(R.string.text_self)+&quot; : &quot;+String.valueOf(seekBar.getProgress()));</p><p> break;</p><p> }</p><p> default:</p><p> break;</p><p> }</p><p> }</p><p>}</p><p>代码说明:</p><p>要监听SeekBar的滑动消息,通过实现&quot;SeekBar.OnSeekBarChangeListener&quot;接口。这个接口中包含3个方法onStartTrackingTouch()、onStopTrackingTouch()和onProgressChanged()。</p><p>layout文件</p><p>&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;</p><p> xmlns:tools=&quot;http://schemas.android.com/tools&quot;</p><p> android:layout_width=&quot;match_parent&quot;</p><p> android:layout_height=&quot;match_parent&quot;</p><p> android:orientation=&quot;vertical&quot; &gt;</p><p> </p><p> &lt;TextView</p><p> android:id=&quot;@+id/tv_def&quot;</p><p> android:layout_width=&quot;wrap_content&quot;</p><p> android:layout_height=&quot;wrap_content&quot;</p><p> android:text=&quot;@string/text_def&quot; /&gt;</p><p> </p><p> &lt;!--</p><p> max=100,代表它的取值范围是0-100,共101个值;</p><p> progress=10,代表默认值是10 </p><p> --&gt;</p><p> &lt;SeekBar</p><p> android:id=&quot;@+id/seekbar_def&quot;</p><p> android:layout_width=&quot;620px&quot;</p><p> android:layout_height=&quot;wrap_content&quot;</p><p> android:max=&quot;100&quot;</p><p> android:progress=&quot;10&quot;</p><p> /&gt;</p><p> </p><p> &lt;TextView</p><p> android:id=&quot;@+id/tv_self&quot;</p><p> android:layout_width=&quot;wrap_content&quot;</p><p> android:layout_height=&quot;wrap_content&quot;</p><p> android:text=&quot;@string/text_self&quot; /&gt;</p><p> </p><p> &lt;!--</p><p> max=100,代表它的取值范围是0-100,共101个值;</p><p> progress=20,代表默认值是20</p><p> progressDrawable,表示SeekBar的背景图片</p><p> thumbe,表示SeekBar的滑块图片 </p><p> --&gt;</p><p> &lt;SeekBar</p><p> android:id=&quot;@+id/seekbar_self&quot;</p><p> android:layout_width=&quot;620px&quot; </p><p> android:layout_height=&quot;wrap_content&quot;</p><p> android:max=&quot;100&quot;</p><p> android:progress=&quot;20&quot;</p><p> android:progressDrawable=&quot;@drawable/bg_bar&quot; </p><p> android:thumb=&quot;@drawable/thumb_bar&quot; /&gt;</p><p> </p><p>&lt;/LinearLayout&gt;</p><p>自定义SeekBar的背景定义为:android:progressDrawable=&quot;@drawable/bg_bar&quot;。</p><p>它调用的bg_bar.xml的内容如下:</p><p>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</p><p>&lt;layer-list xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;</p><p> &lt;!-- 背景图 --&gt;</p><p> &lt;item android:id=&quot;@+android:id/background&quot; android:drawable=&quot;@drawable/bar_dn&quot; /&gt;</p><p> &lt;!-- 第二进度图 --&gt;</p><p> &lt;item android:id=&quot;@+android:id/SecondaryProgress&quot; android:drawable=&quot;@drawable/bar_dn&quot; /&gt;</p><p> &lt;!-- 进度度 --&gt;</p><p> &lt;item android:id=&quot;@+android:id/progress&quot; android:drawable=&quot;@drawable/bar_up&quot; /&gt;</p><p>&lt;/layer-list&gt;</p><p>bar_dn.png如下图:</p><p><img src="/up_pic/201809/021140185592.png" title="021140185592.png" alt="a.png"/></p><p>bar_up.png如下图:</p><p><img src="/up_pic/201809/021140229547.png" title="021140229547.png" alt="b.png"/></p><p>自定义SeekBar的滑块定义为:android:thumb=&quot;@drawable/thumb_bar&quot;。</p><p>它调用的thumb_bar.xml的内容如下:</p><p>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</p><p>&lt;selector xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;</p><p> &lt;!-- 按下状态 --&gt;</p><p> &lt;item android:state_pressed=&quot;true&quot;</p><p> android:drawable=&quot;@drawable/thumb_dn&quot; /&gt;</p><p> &lt;!-- 焦点状态 --&gt;</p><p> &lt;item android:state_focused=&quot;true&quot;</p><p> android:drawable=&quot;@drawable/thumb_up&quot; /&gt;</p><p> </p><p> &lt;!-- 默认状态 --&gt;</p><p> &lt;item android:drawable=&quot;@drawable/thumb_up&quot; /&gt; </p><p> </p><p>&lt;/selector&gt;</p><p>thumb_up.png如下图:</p><p><img src="/up_pic/201809/021140306620.png" title="021140306620.png" alt="1.png"/></p><p>thumb_dn.png如下图:</p><p><img src="/up_pic/201809/021140345275.png" title="021140345275.png" alt="2.png"/></p><p>manifest文件</p><p>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</p><p>&lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;</p><p> package=&quot;com.skywang.control&quot;</p><p> android:versionCode=&quot;1&quot;</p><p> android:versionName=&quot;1.0&quot; &gt;</p><p> &lt;uses-sdk</p><p> android:minSdkVersion=&quot;8&quot;</p><p> android:targetSdkVersion=&quot;17&quot; /&gt;</p><p> &lt;application</p><p> android:allowBackup=&quot;true&quot;</p><p> android:icon=&quot;@drawable/ic_launcher&quot;</p><p> android:label=&quot;@string/app_name&quot;</p><p> android:theme=&quot;@style/AppTheme&quot; &gt;</p><p> &lt;activity</p><p> android:name=&quot;com.skywang.control.SeekBarTest&quot;</p><p> android:label=&quot;@string/app_name&quot; &gt;</p><p> &lt;intent-filter&gt;</p><p> &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;</p><p> &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;</p><p> &lt;/intent-filter&gt;</p><p> &lt;/activity&gt;</p><p> &lt;/application&gt;</p><p>&lt;/manifest&gt;</p><p>点击下载:源代码</p><p>运行效果:如图</p><p><img src="/up_pic/201809/021139057303.jpg" title="021139057303.jpg" alt="3.jpg"/></p>
RangeTime:0.009053s
RangeMem:215.52 KB
返回顶部 留言