<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 2.3.8 SeekBar(拖動條) ## 本節引言: > 本節我們繼續來學習Android的基本UI控件中的拖動條——SeekBar,相信大家對他并不陌生,最常見的 地方就是音樂播放器或者視頻播放器了,音量控制或者播放進度控制,都用到了這個SeekBar,我們 先來看看SeekBar的類結構,來到官方文檔:[SeekBar](http://androiddoc.qiniudn.com/reference/android/widget/SeekBar.html) > > ![](http://www.runoob.com/wp-content/uploads/2015/08/45807422.jpg) > > 嘿嘿,這玩意是ProgressBar的子類耶,也就是ProgressBar的屬性都可以用咯! 而且他還有一個自己的屬性就是:**android:thumb**,就是允許我們自定義滑塊~ 好的,開始本節內容! ## 1.SeekBar基本用法 好吧,基本用法其實很簡單,常用的屬性無非就下面這幾個常用的屬性,Java代碼里只要setXxx即可: > **android:max**="100" //滑動條的最大值 > > **android:progress**="60" //滑動條的當前值 > > **android:secondaryProgress**="70" //二級滑動條的進度 > > **android:thumb** = "@mipmap/sb_icon" //滑塊的drawable 接著要說下SeekBar的事件了,**SeekBar.OnSeekBarChangeListener** 我們只需重寫三個對應的方法: > **onProgressChanged**:進度發生改變時會觸發 > > **onStartTrackingTouch**:按住SeekBar時會觸發 > > **onStopTrackingTouch**:放開SeekBar時觸發 **簡單的代碼示例:** **效果圖:** ![](http://www.runoob.com/wp-content/uploads/2015/08/22291787.jpg) **實現代碼:** ``` public class MainActivity extends AppCompatActivity { private SeekBar sb_normal; private TextView txt_cur; private Context mContext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = MainActivity.this; bindViews(); } private void bindViews() { sb_normal = (SeekBar) findViewById(R.id.sb_normal); txt_cur = (TextView) findViewById(R.id.txt_cur); sb_normal.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { txt_cur.setText("當前進度值:" + progress + " / 100 "); } @Override public void onStartTrackingTouch(SeekBar seekBar) { Toast.makeText(mContext, "觸碰SeekBar", Toast.LENGTH_SHORT).show(); } @Override public void onStopTrackingTouch(SeekBar seekBar) { Toast.makeText(mContext, "放開SeekBar", Toast.LENGTH_SHORT).show(); } }); } } ``` ## 2.簡單SeekBar定制: > 本來還想著自定義下SeekBar的,后來想想,還是算了,涉及到自定義View的一些東西,可能初學者并 不了解,看起來也有點難度,關于自定義View的還是放到進階那里吧,所以這里就只是簡單的定制下SeekBar! 定制的內容包括滑塊,以及軌道! **代碼實例:** **運行效果圖:** ![](http://www.runoob.com/wp-content/uploads/2015/08/34246693.jpg) **代碼實現:** **1.滑塊狀態Drawable:sb_thumb.xml** ``` <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@mipmap/seekbar_thumb_pressed"/> <item android:state_pressed="false" android:drawable="@mipmap/seekbar_thumb_normal"/> </selector> ``` **貼下素材:** ![seekbar_thumb_normal.png](http://www.runoob.com/wp-content/uploads/2015/08/71397914.jpg)![seekbar_thumb_pressed.png](http://www.runoob.com/wp-content/uploads/2015/08/25576180.jpg) **2.條形欄Bar的Drawable:sb_bar.xml** 這里用到一個layer-list的drawable資源!其實就是層疊圖片,依次是:背景,二級進度條,當前進度: ``` <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@android:id/background"> <shape> <solid android:color="#FFFFD042" /> </shape> </item> <item android:id="@android:id/secondaryProgress"> <clip> <shape> <solid android:color="#FFFFFFFF" /> </shape> </clip> </item> <item android:id="@android:id/progress"> <clip> <shape> <solid android:color="#FF96E85D" /> </shape> </clip> </item> </layer-list> ``` **3.然后布局引入SeekBar后,設置下progressDrawable與thumb即可!** ``` <SeekBar android:id="@+id/sb_normal" android:layout_width="match_parent" android:layout_height="wrap_content" android:maxHeight="5.0dp" android:minHeight="5.0dp" android:progressDrawable="@drawable/sb_bar" android:thumb="@drawable/sb_thumb"/> ``` 就是這么簡單! ## 本節小結: 好的,關于SeekBar就到這里,謝謝大家~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看