国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android中ToggleButton開關狀態按鈕控件使用方法詳解

2019-12-12 02:18:28
字體:
來源:轉載
供稿:網友

ToggleButton開關狀態按鈕控件使用方法,具體內容如下

一、簡介

1、

2、ToggleButton類結構

父類是CompoundButton,引包的時候注意下

二、ToggleButton開關狀態按鈕控件使用方法

1、新建ToggleButton控件及對象

private ToggleButton toggleButton1;

toggleButton1=(ToggleButton) findViewById(R.id.toggleButton1);

2、設置setOnCheckedChangeListener方法

toggleButton1.setOnCheckedChangeListener(new OnCheckedChangeListener() {})

3、根據是否checked方法實現操作

if(isChecked){//開  linearLayout1.setOrientation(LinearLayout.VERTICAL);}else{//關  linearLayout1.setOrientation(LinearLayout.HORIZONTAL);} 

三、代碼實例

1、效果圖:

開狀態

關狀態

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開關狀態按鈕控件使用方法     * 1、新建ToggleButton控件及對象     * 2、設置setOnCheckedChangeListener方法     * 3、根據是否checked方法實現操作     *      */    toggleButton1.setOnCheckedChangeListener(new OnCheckedChangeListener() {            @Override      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {        // TODO Auto-generated method stub        if(isChecked){//開          linearLayout1.setOrientation(LinearLayout.VERTICAL);        }        else{//關          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"

設置ToggleButton 狀態

2、

android:textOn="橫向排列"

設置ToggleButton打開文本

3、

toggleButton1.setOnCheckedChangeListener(new OnCheckedChangeListener() {})

設置ToggleButton的setOnCheckedChangeListener方法

4、

if(isChecked)

判斷ToggleButton狀態開關

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 蒲城县| 芜湖县| 洛扎县| 宝清县| 龙海市| 山丹县| 格尔木市| 白玉县| 汕头市| 车致| 靖西县| 安康市| 朔州市| 沐川县| 藁城市| 吉安市| 永兴县| 色达县| 海盐县| 南通市| 化德县| 中阳县| 车险| 东阿县| 新田县| 右玉县| 六安市| 叶城县| 洛扎县| 泽州县| 新建县| 得荣县| 琼结县| 班玛县| 赣榆县| 凤城市| 嘉善县| 华亭县| 郯城县| 阿拉善右旗| 获嘉县|