您好,登錄后才能下訂單哦!
Android ToggleButton 詳解
在Android的開發(fā)過程中,對(duì)于ToggleButton的使用頻率也是相當(dāng)?shù)母叩模旅嫖揖蛠碚f一下,這個(gè)組件的兩種使用方式。
第一種是簡(jiǎn)單的使用,利用Toast的方式彈出提示語句
需要注意的是要想自定義ToggleButton的顯示的內(nèi)容,就需要設(shè)置其TextOn和TextOff的內(nèi)容。
<ToggleButton android:id="@+id/toggleButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/toggleButton2" android:layout_alignBottom="@+id/toggleButton2" android:textOn="開" android:textOff="關(guān)" android:layout_alignRight="@+id/imageview" android:text="Simple test" />
然后是主要的顯示代碼:
case R.id.toggleButton1: if(SimpleTest.isChecked()){ Toast.makeText(getApplication(), "你打開了開按鈕", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(getApplication(), "你打開了關(guān)按鈕", Toast.LENGTH_SHORT).show(); } break; //應(yīng)該注意的是,先聲明ToggleButton并初始化,然后注冊(cè)偵聽方法
接下來是一個(gè)較為復(fù)雜一點(diǎn)的使用案例,那就是配合ImageView來實(shí)現(xiàn)不同的圖片顯示狀態(tài)
<ToggleButton android:id="@+id/toggleButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/imageview" android:layout_alignParentTop="true" android:layout_marginTop="46dp" android:textOn="美女" android:textOff="圖標(biāo)" android:text="With Image" /> <ImageView android:id="@+id/imageview" android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@drawable/note" android:layout_below="@id/toggleButton2" />
然后是活動(dòng)代碼
case R.id.toggleButton2: if(WithImage.isChecked()){ imageview.setImageResource(R.drawable.note); }else{ imageview.setImageResource(R.drawable.ic_launcher); } break;
需要注意的是,我們同樣需要先進(jìn)行聲明,才能對(duì)其使用,否則會(huì)報(bào)空指針的錯(cuò)誤。
下面是程序運(yùn)行之后的結(jié)果
總結(jié)與設(shè)想:
在使用過程中使用到的ToggleButton 一般來說不會(huì)這么的簡(jiǎn)單,但是主要的思想和框架還是基于這里的。我們可以在相關(guān)的偵聽方法中添加比如靜音的處理,或者status的改變等等。這樣,我們的應(yīng)用就會(huì)變得更加的靈活了。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。