Android入門之SwitchButton的使用教程
介紹
SwitchButton是個(gè)什么樣的東西呢?其實(shí)它就是一個(gè)開關(guān)。我們在手機(jī)應(yīng)用中經(jīng)常使用到的。我突然想到2012年我開發(fā)Android時(shí),竟然使用了RadioButton來做開關(guān)這個(gè)梗。
其實(shí)SwitchButton文如其名,它就是一個(gè)“開”和“關(guān)”兩個(gè)狀態(tài)事件存在時(shí)使用的,最典型的SwitchButton長下面這么個(gè)樣子。

課程目標(biāo)
制作一個(gè)類似于IOS里的SwitchButton效果的按鈕。并且打印出它當(dāng)前的狀態(tài)。
Android Studio自帶的SwitchButton樣式非常的難看,達(dá)不到我們要的效果,它如果直接拖到我們的Android Studio里是一個(gè)灰白色的按鈕。我們這次把這個(gè)按鈕美化一下。
其實(shí)美化不用太精致,必竟我們不是專業(yè)UI,因此開發(fā)者們一定不要一開始沉醉于界面樣式的太Beautiful,我們主要核心就是把Android開發(fā)全給掌握了,并且它是為我們本身的JAVA中臺(tái)、大數(shù)據(jù)、AI開發(fā)能力來服務(wù)的。因?yàn)槟汩_發(fā)的很多后臺(tái)功能,如果對(duì)于一些大領(lǐng)導(dǎo)或者是業(yè)務(wù)型領(lǐng)導(dǎo),他們是看不到你的“真實(shí)能力的”,他們更多關(guān)注的是“功能長什么樣”,因此如果直接可以讓他們看到在手機(jī)里運(yùn)行起來這個(gè)人臉、這個(gè)物品識(shí)別、這個(gè)用手機(jī)設(shè)藍(lán)牙鎖開鎖等過程你可以省卻很多無用的BLA BLA。同時(shí)還能為自己將來進(jìn)一步通往IOT領(lǐng)域打下堅(jiān)實(shí)的基礎(chǔ)。因此在我的教程里不會(huì)太多講如何的專業(yè)UI。
為了滿足我們基本的UI,我們需要知道SwitchButton的外觀受以下幾個(gè)事件的影響,它們是:
- android:thumb,SwithButton上的這個(gè)“圓點(diǎn)點(diǎn)”的外觀,再分on時(shí)長什么樣、off時(shí)長什么樣;
- android:track,SwitchButton圓點(diǎn)點(diǎn)的背后一個(gè)長橢圓型的導(dǎo)軌的樣式,再分按鈕是on時(shí)導(dǎo)軌是綠的、off時(shí)導(dǎo)軌是淺灰的;
我們自定義這兩個(gè)UI部分即可實(shí)現(xiàn)。
自定義SwitchButton的Thumb和Track
自定義Thumb
它需要兩個(gè)xml文件:
- thumb_on
- thumb_off
然后把這兩個(gè)xml文件合到一個(gè)selector的xml文件內(nèi)申明成on時(shí)用哪個(gè)文件、off時(shí)用哪個(gè)文件就能起到switch button的這個(gè)圓點(diǎn)點(diǎn)樣式的自定了。
在此,我們不會(huì)使用外部圖片.png等資源,而只用android里提供的簡單的shape來作這個(gè)圖形的繪制,它相當(dāng)?shù)暮唵?/p>
switch_custom_thumb_on.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#94C5FF" />
<size
android:width="40dp"
android:height="40dp" />
</shape>switch_custom_thumb_off.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#AAA" />
<size
android:width="40dp"
android:height="40dp" />
</shape>switch_custom_thumb_selector.xml文件
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/switch_custom_thumb_on" android:state_checked="true" />
<item android:drawable="@drawable/switch_custom_thumb_off" android:state_checked="false" />
</selector>開發(fā)這一塊代碼時(shí)我們使用Android Studio里的Split代碼與樣式預(yù)覽集成可以動(dòng)態(tài)看到我們在改代碼時(shí)右邊圖形發(fā)生的變化。
這邊的thumb就是給你的“手指”按的圓點(diǎn)點(diǎn)我們使用的是android:shape="oval",藍(lán)色。

自定義Track
導(dǎo)軌我們也使用藍(lán)色,它同樣也是分成:
- switch_custom_track_on.xml
- switch_custom_track_off.xml
然后同樣也在一個(gè)selector xml文件內(nèi)申明成on時(shí)用哪個(gè)文件、off時(shí)用哪個(gè)文件。
switch_custom_track_on.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#B6D6FE" />
<stroke
android:width="5dp"
android:color="#00000000" />
<corners android:radius="20dp" />
</shape>switch_custom_track_off.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#E3E3E3" />
<stroke
android:width="5dp"
android:color="#00000000" />
<corners android:radius="20dp" />
</shape>switch_custom_track_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/switch_custom_track_on" android:state_checked="true" />
<item android:drawable="@drawable/switch_custom_track_off" android:state_checked="false" />
</selector>好,我們以上把按鈕和導(dǎo)軌都定義好了,我們來看主UI界面。
主UI界面activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Switch android:id="@+id/switchBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:thumb="@drawable/switch_custom_thumb_selector"
android:track="@drawable/switch_custom_track_selector" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>看,以上代碼里我們對(duì)android:thumb和android:track使用了這兩個(gè)自定義on/off樣式的xml文件。
它在界面上會(huì)長成這么個(gè)樣。

我們來看這個(gè)按鈕的事件是如何響應(yīng)的。
SwitchButton交互事件發(fā)生時(shí)的代碼
MainActivity.java
package org.mk.android.demo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.CompoundButton;
import android.widget.Switch;
public class MainActivity extends AppCompatActivity {
private Switch btnSwitch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSwitch = (Switch) findViewById(R.id.switchBtn);
btnSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
Log.i("app", ">>>>>>switched is on");
} else {
Log.i("app", ">>>>>>switched is off");
}
}
});
}
}我們對(duì)SwitchButton的onCheckedChanged進(jìn)行了自定義,它運(yùn)行起來會(huì)是這樣的一個(gè)效果。
運(yùn)行效果
開關(guān)off時(shí)

開關(guān)on時(shí)

到此這篇關(guān)于Android入門之SwitchButton的使用教程的文章就介紹到這了,更多相關(guān)Android SwitchButton內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
為Android系統(tǒng)添加config.xml 新配置的設(shè)置
這篇文章主要介紹了為Android系統(tǒng)添加config.xml 新配置的設(shè)置,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03
flutter實(shí)現(xiàn)帶刪除動(dòng)畫的listview功能
最近接了一個(gè)新項(xiàng)目,需要開發(fā)帶有刪除動(dòng)畫效果的listview功能,在實(shí)現(xiàn)過程中列表滾動(dòng)效果用listview實(shí)現(xiàn)的,本文通過實(shí)例代碼給大家分享實(shí)現(xiàn)過程,感興趣的朋友跟隨小編一起學(xué)習(xí)下吧2021-05-05
Android 5.0+ 屏幕錄制實(shí)現(xiàn)的示例代碼
這篇文章主要介紹了Android 5.0+ 屏幕錄制實(shí)現(xiàn)的示例代碼,從 5.0 開始,系統(tǒng)提供給了 app 錄制屏幕的一系列方法,不需要 root 權(quán)限,只需要用戶授權(quán)即可錄屏,相對(duì)來說較為簡單,感興趣的小伙伴們可以參考一下2018-05-05
Android實(shí)現(xiàn)捕獲TextView超鏈接的方法
這篇文章主要介紹了Android實(shí)現(xiàn)捕獲TextView超鏈接的方法,涉及Android查找TextView中超鏈接的相關(guān)實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
Android開發(fā)框架之自定義ZXing二維碼掃描界面并解決取景框拉伸問題
這篇文章主要介紹了Android開發(fā)框架之自定義ZXing二維碼掃描界面并解決取景框拉伸問題的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06
Android Surfaceview的繪制與應(yīng)用
這篇文章主要介紹了Android Surfaceview的繪制與應(yīng)用的相關(guān)資料,需要的朋友可以參考下2017-07-07

