W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
本節(jié)給大家介紹的Android基本UI控件是:開(kāi)關(guān)按鈕ToggleButton和開(kāi)關(guān)Switch,可能大家對(duì)著兩個(gè)組件 并不熟悉,突然想起筆者的第一間外包公司,是否在wifi下聯(lián)網(wǎng)的開(kāi)關(guān),竟然用的TextView,然后叫美工 且兩個(gè)切換前后的圖,然后代碼中進(jìn)行設(shè)置,當(dāng)然點(diǎn)擊TextView的時(shí)候判斷狀態(tài),然后設(shè)置對(duì)應(yīng)的背景...好吧,也是醉了,好吧...本節(jié)講解的兩個(gè)其實(shí)都是開(kāi)關(guān)組件,只是后者需要在Android 4.0以后才能使用 所以AndroidManifest.xml文件中的minsdk需要 >= 14 否則會(huì)報(bào)錯(cuò)~,先來(lái)看看這兩個(gè)控件長(zhǎng)什么樣先, Android 5.0后這兩個(gè)控件相比以前來(lái)說(shuō)好看了許多,先看下5.0前的樣子:5.0以前的ToggleButton和Switch: 5.0版本:好吧,鮮明的對(duì)比...接下來(lái)我們就來(lái)學(xué)習(xí)者兩個(gè)控件的使用吧,其實(shí)兩個(gè)的使用幾乎是相同的。開(kāi)始之前貼下官方API先:Switch;ToggleButton
可供我們?cè)O(shè)置的屬性:
android:disabledAlpha:設(shè)置按鈕在禁用時(shí)的透明度android:textOff:按鈕沒(méi)有被選中時(shí)顯示的文字android:textOn:按鈕被選中時(shí)顯示的文字 另外,除了這個(gè)我們還可以自己寫個(gè)selector,然后設(shè)置下Background屬性即可~
可供我們?cè)O(shè)置的屬性:
android:showText:設(shè)置on/off的時(shí)候是否顯示文字,booleanandroid:splitTrack:是否設(shè)置一個(gè)間隙,讓滑塊與底部圖片分隔,booleanandroid:switchMinWidth:設(shè)置開(kāi)關(guān)的最小寬度android:switchPadding:設(shè)置滑塊內(nèi)文字的間隔android:switchTextAppearance:設(shè)置開(kāi)關(guān)的文字外觀,暫時(shí)沒(méi)發(fā)現(xiàn)有什么用...android:textOff:按鈕沒(méi)有被選中時(shí)顯示的文字android:textOn:按鈕被選中時(shí)顯示的文字android:textStyle:文字風(fēng)格,粗體,斜體寫劃線那些android:track:底部的圖片android:thumb:滑塊的圖片android:typeface:設(shè)置字體,默認(rèn)支持這三種:sans, serif, monospace;除此以外還可以使用 其他字體文件(*.ttf),首先要將字體文件保存在assets/fonts/目錄下,不過(guò)需要在Java代碼中設(shè)置: Typeface typeFace =Typeface.createFromAsset(getAssets(),"fonts/HandmadeTypewriter.ttf"); textView.setTypeface(typeFace);
因?yàn)楸容^簡(jiǎn)單,所以我們把他們寫到一起,另外,我們?yōu)镾witch設(shè)置下滑塊和底部的圖片,實(shí)現(xiàn) 一個(gè)類似于IOS 7的滑塊的效果,但是有個(gè)缺點(diǎn)就是不能在XML中對(duì)滑塊和底部的大小進(jìn)行設(shè)置, 就是素材多大,Switch就會(huì)多大,我們可以在Java中獲得Drawable對(duì)象,然后對(duì)大小進(jìn)行修改, 簡(jiǎn)單的例子:
運(yùn)行效果圖:
實(shí)現(xiàn)代碼: 先是兩個(gè)drawable的文件: thumb_selctor.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="@drawable/switch_btn_pressed"/>
<item android:state_pressed="false" android:drawable="@drawable/switch_btn_normal"/>
</selector>
track_selctor.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/switch_btn_bg_green"/>
<item android:state_checked="false" android:drawable="@drawable/switch_btn_bg_white"/>
</selector>
布局文件:activity_main.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:orientation="vertical"
tools:context=".MainActivity">
?
<ToggleButton
android:id="@+id/tbtn_open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:textOff="關(guān)閉聲音"
android:textOn="打開(kāi)聲音" />
?
<Switch
android:id="@+id/swh_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff=""
android:textOn=""
android:thumb="@drawable/thumb_selctor"
android:track="@drawable/track_selctor" />
?
</LinearLayout>
MainActivity.java:
public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener{
?
private ToggleButton tbtn_open;
private Switch swh_status;
?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
?
tbtn_open = (ToggleButton) findViewById(R.id.tbtn_open);
swh_status = (Switch) findViewById(R.id.swh_status);
tbtn_open.setOnCheckedChangeListener(this);
swh_status.setOnCheckedChangeListener(this);
}
?
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
switch (compoundButton.getId()){
case R.id.tbtn_open:
if(compoundButton.isChecked()) Toast.makeText(this,"打開(kāi)聲音",Toast.LENGTH_SHORT).show();
else Toast.makeText(this,"打開(kāi)聲音",Toast.LENGTH_SHORT).show();
break;
case R.id.swh_status:
if(compoundButton.isChecked()) Toast.makeText(this,"開(kāi)關(guān):ON",Toast.LENGTH_SHORT).show();
else Toast.makeText(this,"開(kāi)關(guān):OFF",Toast.LENGTH_SHORT).show();
break;
?
}
}
}
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: