Android ToggleButton 詳解及實(shí)例代碼
Android ToggleButton 詳解
在Android的開發(fā)過程中,對于ToggleButton的使用頻率也是相當(dāng)?shù)母叩?,下面我就來說一下,這個(gè)組件的兩種使用方式。
第一種是簡單的使用,利用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并初始化,然后注冊偵聽方法
接下來是一個(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)行聲明,才能對其使用,否則會(huì)報(bào)空指針的錯(cuò)誤。
下面是程序運(yùn)行之后的結(jié)果


總結(jié)與設(shè)想:
在使用過程中使用到的ToggleButton 一般來說不會(huì)這么的簡單,但是主要的思想和框架還是基于這里的。我們可以在相關(guān)的偵聽方法中添加比如靜音的處理,或者status的改變等等。這樣,我們的應(yīng)用就會(huì)變得更加的靈活了。
- Android控件之ToggleButton的使用方法
- android基本控件ToggleButton&Switch使用指南
- Android控件ToggleButton多狀態(tài)按鈕使用詳解
- Android中ToggleButton開關(guān)狀態(tài)按鈕控件使用方法詳解
- Android開發(fā)之ToggleButton實(shí)現(xiàn)開關(guān)效果示例
- Android自定義實(shí)現(xiàn)開關(guān)按鈕代碼
- Android自定義控件實(shí)現(xiàn)滑動(dòng)開關(guān)效果
- Android 仿蘋果IOS6開關(guān)按鈕
- Android開發(fā)進(jìn)階自定義控件之滑動(dòng)開關(guān)實(shí)現(xiàn)方法【附demo源碼下載】
- Android開發(fā)仿IOS滑動(dòng)開關(guān)實(shí)現(xiàn)代碼
- Android開發(fā)之開關(guān)按鈕控件ToggleButton簡單用法示例
相關(guān)文章
詳解Android?Flutter如何使用相機(jī)實(shí)現(xiàn)拍攝照片
在app中使用相機(jī)肯定是再平常不過的一項(xiàng)事情了,相機(jī)肯定涉及到了底層原生代碼的調(diào)用,那么在flutter中如何快速簡單的使用上相機(jī)的功能呢?一起來看看吧2023-04-04
Android利用ObjectAnimator實(shí)現(xiàn)ArcMenu
這篇文章主要為大家詳細(xì)介紹了Android利用ObjectAnimator實(shí)現(xiàn)ArcMenu的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-07-07
android studio3.0以上如何通過navicat訪問SQLite數(shù)據(jù)庫文件
這篇文章主要介紹了android studio3.0以上如何通過navicat訪問SQLite數(shù)據(jù)庫文件,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
monkeyrunner之安卓開發(fā)環(huán)境搭建教程(1)
這篇文章主要介紹了monkeyrunner之安卓開發(fā)環(huán)境搭建教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Android中實(shí)現(xiàn)Runnable接口簡單例子
詳解Android中OkHttp3的例子和在子線程更新UI線程的方法
詳解OpenGL Shader抗鋸齒的實(shí)現(xiàn)

