Android中ToggleButton開關(guān)狀態(tài)按鈕控件使用方法詳解
ToggleButton開關(guān)狀態(tài)按鈕控件使用方法,具體內(nèi)容如下
一、簡(jiǎn)介
1、

2、ToggleButton類結(jié)構(gòu)

父類是CompoundButton,引包的時(shí)候注意下
二、ToggleButton開關(guān)狀態(tài)按鈕控件使用方法
1、新建ToggleButton控件及對(duì)象
private ToggleButton toggleButton1;
toggleButton1=(ToggleButton) findViewById(R.id.toggleButton1);
2、設(shè)置setOnCheckedChangeListener方法
toggleButton1.setOnCheckedChangeListener(new OnCheckedChangeListener() {})
3、根據(jù)是否checked方法實(shí)現(xiàn)操作
if(isChecked){//開
linearLayout1.setOrientation(LinearLayout.VERTICAL);
}
else{//關(guān)
linearLayout1.setOrientation(LinearLayout.HORIZONTAL);
}
三、代碼實(shí)例
1、效果圖:
開狀態(tài)

關(guān)狀態(tài)

2、代碼:
fry.Activity01
package fry;
import com.example.ToggleButtonDemo1.R;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.ToggleButton;
public class Activity01 extends Activity{
private LinearLayout linearLayout1;
private ToggleButton toggleButton1;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity01);
linearLayout1=(LinearLayout) findViewById(R.id.linearLayout1);
toggleButton1=(ToggleButton) findViewById(R.id.toggleButton1);
/*
* ToggleButton開關(guān)狀態(tài)按鈕控件使用方法
* 1、新建ToggleButton控件及對(duì)象
* 2、設(shè)置setOnCheckedChangeListener方法
* 3、根據(jù)是否checked方法實(shí)現(xiàn)操作
*
*/
toggleButton1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){//開
linearLayout1.setOrientation(LinearLayout.VERTICAL);
}
else{//關(guān)
linearLayout1.setOrientation(LinearLayout.HORIZONTAL);
}
}
});
}
}
/ToggleButtonDemo1/res/layout/activity01.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ToggleButton
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:textOn="橫向排列"
android:textOff="縱向排列"
/>
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button"
/>
</LinearLayout>
</LinearLayout>
四、獲得
1、
android:checked="true"
設(shè)置ToggleButton 狀態(tài)
2、
android:textOn="橫向排列"
設(shè)置ToggleButton打開文本
3、
toggleButton1.setOnCheckedChangeListener(new OnCheckedChangeListener() {})
設(shè)置ToggleButton的setOnCheckedChangeListener方法
4、
if(isChecked)
判斷ToggleButton狀態(tài)開關(guān)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android開發(fā)之開關(guān)按鈕用法示例
- Android開發(fā)之開關(guān)按鈕控件ToggleButton簡(jiǎn)單用法示例
- Android 自定義Switch開關(guān)按鈕的樣式實(shí)例詳解
- Android基于ImageView繪制的開關(guān)按鈕效果示例
- Android動(dòng)畫 實(shí)現(xiàn)開關(guān)按鈕動(dòng)畫(屬性動(dòng)畫之平移動(dòng)畫)實(shí)例代碼
- Android自定義View實(shí)現(xiàn)開關(guān)按鈕
- Android 仿蘋果IOS6開關(guān)按鈕
- Android模擬開關(guān)按鈕點(diǎn)擊打開動(dòng)畫(屬性動(dòng)畫之平移動(dòng)畫)
- Android自定義實(shí)現(xiàn)開關(guān)按鈕代碼
- Android自定義開關(guān)按鈕源碼解析
相關(guān)文章
Android如何使用Flow封裝一個(gè)FlowBus工具類
本文介紹了如何在Android中使用Flow封裝一個(gè)FlowBus工具類,以替代EvenutBus、Rxbus、LiveDataBus、LiveData等第三方依賴包,作者提供了在Activity、Fragment、Service和Websock中使用FlowBus的具體代碼,并解釋了實(shí)現(xiàn)的效果,文章最后還提供了項(xiàng)目demo源碼的下載鏈接2024-09-09
Android實(shí)現(xiàn)計(jì)時(shí)與倒計(jì)時(shí)的方法匯總
這篇文章主要介紹了Android實(shí)現(xiàn)計(jì)時(shí)與倒計(jì)時(shí)的方法匯總,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-06-06
Android.mk引入第三方j(luò)ar包和so庫文件的方法
這篇文章主要介紹了Android.mk引入第三方j(luò)ar包和so庫文件的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05
Android實(shí)現(xiàn)滑塊拼圖驗(yàn)證碼功能
這篇文章主要介紹了Android實(shí)現(xiàn)滑塊拼圖驗(yàn)證碼功能,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
Android中在WebView里實(shí)現(xiàn)Javascript調(diào)用Java類的方法
這篇文章主要介紹了Android中在WebView里實(shí)現(xiàn)Javascript調(diào)用Java類的方法,本文直接給出示例,需要的朋友可以參考下2015-03-03
Android StrictMode運(yùn)行流程(推薦)
strictmode是android在 API9后引入的檢測(cè)影響app運(yùn)行流暢性的一種機(jī)制。這篇文章給大家介紹了android strictmode運(yùn)行流程,需要的朋友參考下吧2018-01-01
Flexbox+ReclyclerView實(shí)現(xiàn)流式布局
這篇文章主要為大家詳細(xì)介紹了Flexbox+ReclyclerView實(shí)現(xiàn)流式布局,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
Android輸入框添加emoje表情圖標(biāo)的實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android輸入框添加emoje表情圖標(biāo)的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
android輸入框與文本框加滾動(dòng)條scrollview示例
這篇文章主要介紹了android輸入框與文本框加滾動(dòng)條scrollview示例,需要的朋友可以參考下2014-05-05
Android深入探究自定義View之嵌套滑動(dòng)的實(shí)現(xiàn)
什么是嵌套滑動(dòng)?當(dāng)我們向下滑動(dòng)時(shí),首先是外部的布局向下滑動(dòng),然后才是內(nèi)部的RecyclerView滑動(dòng),向上滑動(dòng)也是如此。這就是嵌套滑動(dòng)的效果2021-11-11

