Android開(kāi)發(fā)之開(kāi)關(guān)按鈕用法示例
本文實(shí)例講述了Android開(kāi)發(fā)之開(kāi)關(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">
<!--定義一個(gè)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"/>
<!--定義一個(gè)可以動(dòng)態(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>
活動(dòng)代碼實(shí)現(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) {
//設(shè)置LinearLayout垂直布局
text.setOrientation(LinearLayout.VERTICAL);
toggle.setChecked(true);
switcher.setChecked(true);
}else {
//設(shè)置水平布局
text.setOrientation(LinearLayout.HORIZONTAL);
toggle.setChecked(false);
switcher.setChecked(false);
}
}
};
toggle.setOnCheckedChangeListener(onCheckedChangeListener);
switcher.setOnCheckedChangeListener(onCheckedChangeListener);
}
}
其中switch組建的 thumb:@drawable/thumb項(xiàng)參考自://www.dhdzp.com/article/159100.htm
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Android控件用法總結(jié)》、《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android數(shù)據(jù)庫(kù)操作技巧總結(jié)》及《Android資源操作技巧匯總》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android解析JSON格式數(shù)據(jù)的兩種方式(JSONObject和Gson)
json數(shù)據(jù)的解析相對(duì)而言,還是比較容易的,實(shí)現(xiàn)的代碼也十分簡(jiǎn)單,下面這篇文章主要給大家介紹了關(guān)于Android解析JSON格式數(shù)據(jù)的兩種方式,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07
Android應(yīng)用中Back鍵的監(jiān)聽(tīng)及處理實(shí)例
在Android應(yīng)用中處理Back鍵按下事件,多種實(shí)現(xiàn)方法如下,感興趣的朋友可以了解下哈2013-06-06
Android實(shí)現(xiàn)使用微信登錄第三方APP的方法
這篇文章主要介紹了Android實(shí)現(xiàn)使用微信登錄第三方APP的方法,結(jié)合實(shí)例形式分析了Android微信登錄APP的操作步驟與具體功能實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-11-11
Kotlin協(xié)程Dispatchers原理示例詳解
這篇文章主要為大家介紹了Kotlin協(xié)程Dispatchers原理示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
Android與iOS包體優(yōu)化及一鍵自動(dòng)打包腳本
這篇文章主要為大家介紹了安卓與iOS包體優(yōu)化及一鍵自動(dòng)打包腳本詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Flutter插件開(kāi)發(fā)之HmsScanKit實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了Flutter插件開(kāi)發(fā)之HmsScanKit實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Kotlin?協(xié)程的取消機(jī)制詳細(xì)解讀
這篇文章主要為大家介紹了Kotlin?協(xié)程的取消機(jī)制詳細(xì)解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
Android使用OKhttp3實(shí)現(xiàn)登錄注冊(cè)功能+springboot搭建后端的詳細(xì)過(guò)程
這篇教程主要實(shí)現(xiàn)Android使用OKhttp3實(shí)現(xiàn)登錄注冊(cè)的功能,后端使用SSM框架,本文通過(guò)實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),需要的朋友參考下吧2021-07-07

