Android基礎控制項SeekBar拖動條的使用

來源:互聯網
上載者:User

標籤:cal   sse   拖動   create   stop   secondary   sed   item   布局   

1、簡介

  SeekBar繼承ProgressBar,相關屬性和三種不同狀態下的觸發方法:

        <!--<SeekBar-->            <!--android:layout_width="match_parent"-->            <!--android:layout_height="wrap_content"-->            <!--android:max="100"-->            <!--android:progress="30"-->            <!--android:id="@+id/seekbar"-->            <!--android:thumb="@mipmap/ic_launcher_round"/>-->//設定滑塊的
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {            @Override//進度發生改變            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {            }            @Override//按下            public void onStartTrackingTouch(SeekBar seekBar) {            }            @Override//放開            public void onStopTrackingTouch(SeekBar seekBar) {            }        });

 

2、簡單使用

  這是一個簡單自訂的SeekBar,淺綠色是進度條顏色,紅色是二級進度條顏色,黃色是背景色!

  滑塊的selector檔案:

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_pressed="true"        android:drawable="@drawable/seekbar_thumb_pressed" />    <item android:state_pressed="false"        android:drawable="@drawable/seekbar_thumb_normal"/></selector>

  進度條顏色layer-list檔案: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="#f23f21" />            </shape>        </clip>    </item>    <item android:id="@android:id/progress">        <clip>            <shape>                <solid android:color="#FF96E85D" />            </shape>        </clip>    </item></layer-list>

  整體xml布局檔案:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:gravity="center"    android:orientation="vertical"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".LoginActivity">        <TextView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="當前進度值:30"            android:id="@+id/textview"/>        <SeekBar            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:id="@+id/seekbar"            android:progress="30"            android:secondaryProgress="50"            android:maxHeight="5dp"            android:minHeight="5dp"            android:progressDrawable="@drawable/sb_bar"            android:thumb="@drawable/sb_thumb"/></LinearLayout>

  Java檔案三種狀態事件的觸發:

public class LoginActivity extends AppCompatActivity  {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_login);        // Set up the login form.        SeekBar seekBar = (SeekBar)findViewById(R.id.seekbar);        final TextView textView = (TextView)findViewById(R.id.textview);        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {            @Override//進度發生改變            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {                textView.setText("當前進度值:"+i);            }            @Override//按下            public void onStartTrackingTouch(SeekBar seekBar) {                Toast.makeText(LoginActivity.this,"按下",Toast.LENGTH_SHORT).show();            }            @Override//放開            public void onStopTrackingTouch(SeekBar seekBar) {                Toast.makeText(LoginActivity.this,"放開",Toast.LENGTH_SHORT).show();            }        });    }}

 

Android基礎控制項SeekBar拖動條的使用

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.