Android開發(fā)之開關(guān)按鈕用法示例
本文實例講述了Android開發(fā)之開關(guān)按鈕用法。分享給大家供大家參考,具體如下:
效果如下:

以下是布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--定義一個ToggleButton按鈕-->
<ToggleButton
android:id="@+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="橫向排列"
android:textOn="縱向排列"
android:checked="true"/>
<Switch
android:id="@+id/switcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="橫向排列"
android:textOn="縱向排列"
android:thumb="@drawable/thumb"
android:checked="true"/>
<!--定義一個可以動態(tài)改變方向的線性布局-->
<LinearLayout
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button03"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
活動代碼實現(xiàn):
public class Home extends AppCompatActivity {
ToggleButton toggle ;
Switch switcher ;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);//顯示manLayout
toggle = (ToggleButton) findViewById(R.id.toggle);
switcher = (Switch) findViewById(R.id.switcher);
final LinearLayout text = (LinearLayout) findViewById(R.id.text);
CompoundButton.OnCheckedChangeListener onCheckedChangeListener =
new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(
CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
//設置LinearLayout垂直布局
text.setOrientation(LinearLayout.VERTICAL);
toggle.setChecked(true);
switcher.setChecked(true);
}else {
//設置水平布局
text.setOrientation(LinearLayout.HORIZONTAL);
toggle.setChecked(false);
switcher.setChecked(false);
}
}
};
toggle.setOnCheckedChangeListener(onCheckedChangeListener);
switcher.setOnCheckedChangeListener(onCheckedChangeListener);
}
}
其中switch組建的 thumb:@drawable/thumb項參考自://www.dbjr.com.cn/article/159100.htm
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android控件用法總結(jié)》、《Android開發(fā)入門與進階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android數(shù)據(jù)庫操作技巧總結(jié)》及《Android資源操作技巧匯總》
希望本文所述對大家Android程序設計有所幫助。
相關(guān)文章
Android實現(xiàn)音量調(diào)節(jié)的方法
這篇文章主要介紹了Android實現(xiàn)音量調(diào)節(jié)的方法,涉及Android頁面布局及多媒體播放的設置技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-09-09
解決Android SELinux權(quán)限問題記錄分析
這篇文章主要為大家介紹了解決Android SELinux權(quán)限問題記錄分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11
詳解 Kotlin Reference Basic Types, String, Array and Imports
這篇文章主要介紹了詳解 Kotlin Reference Basic Types, String, Array and Imports的相關(guān)資料,需要的朋友可以參考下2017-06-06
Android中協(xié)調(diào)滾動布局的實現(xiàn)代碼
這篇文章主要介紹了Android中協(xié)調(diào)滾動常用的布局實現(xiàn),類似這樣的協(xié)調(diào)滾動布局,當?shù)撞苛斜砘瑒拥臅r候,頂部的布局做響應的動作,我們都可以通過?AppBarLayout?和?MotionLayout?來實現(xiàn),本文通過實例代碼介紹的非常詳細,需要的朋友參考下吧2022-06-06
Android 廣播監(jiān)聽網(wǎng)絡狀態(tài)詳解及實例代碼
這篇文章主要介紹了Android 廣播監(jiān)聽網(wǎng)絡狀態(tài)詳解及實例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02

