欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android中ToggleButton開關(guān)狀態(tài)按鈕控件使用方法詳解

 更新時間:2021年08月25日 11:40:05   作者:飯飯_fan  
這篇文章主要為大家詳細(xì)介紹了Android中ToggleButton開關(guān)狀態(tài)按鈕控件的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

ToggleButton開關(guān)狀態(tài)按鈕控件使用方法,具體內(nèi)容如下

一、簡介

1、

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

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

二、ToggleButton開關(guān)狀態(tài)按鈕控件使用方法

1、新建ToggleButton控件及對象

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控件及對象
     * 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)

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論