最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

android基本控件ToggleButton&Switch使用指南

 更新時間:2016年01月09日 08:46:56   投稿:hebedich  
本文給大家匯總介紹了android的2個基本控件ToggleButton和Switch的使用方法,非常的詳細(xì),有需要的小伙伴可以參考下。

ToggleButton(開關(guān)按鈕)和Switch(開關(guān))講解:

一、核心屬性講解:

(1)ToggleButton

textOn:按鈕被選中的時候文字顯示

textOff:按鈕沒有被選中的時候文字顯示

ToggleButton的狀態(tài)只能是選中和未選中,并且需要為不同的狀態(tài)設(shè)置不同的顯示文本。

以下案例為ToggleButton的用法

目錄結(jié)構(gòu)

main.xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <ImageView android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/bulb_off" 
    android:layout_gravity="center_horizontal" />
  <ToggleButton android:id="@+id/toggleButton"
    android:layout_width="140dip"
    android:layout_height="wrap_content"
    android:textOn="開燈"
    android:textOff="關(guān)燈"
    android:layout_gravity="center_horizontal" />
</LinearLayout>

ToggleButtonActivity類

package com.ljq.tb;

import android.app.Activity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ToggleButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class ToggleButtonActivity extends Activity {
  private ImageView imageView=null;
  private ToggleButton toggleButton=null;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    
    imageView=(ImageView) findViewById(R.id.imageView);
    toggleButton=(ToggleButton)findViewById(R.id.toggleButton);
    toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener(){

      public void onCheckedChanged(CompoundButton buttonView,
          boolean isChecked) {
        toggleButton.setChecked(isChecked);
        imageView.setImageResource(isChecked?R.drawable.bulb_on:R.drawable.bulb_off);
      }
      
    });
  }
}

運行效果:

(2)switch:

showText:設(shè)置textOn/off的時候文字是否顯示

android:showText:設(shè)置on/off的時候是否顯示文字,boolean

android:splitTrack:是否設(shè)置一個間隙,讓滑塊與底部圖片分隔,boolean

android:switchMinWidth:設(shè)置開關(guān)的最小寬度

android:switchPadding:設(shè)置滑塊內(nèi)文字的間隔

android:textOff:按鈕沒有被選中時顯示的文字

android:textOn:按鈕被選中時顯示的文字

android:textStyle:文字風(fēng)格,粗體,斜體寫劃線那些

android:track:底部的圖片

android:thumb:滑塊的圖片

可以自己動手試一試每一個屬性

在做一個藍牙開關(guān)時候,用到了switch,記一下用法,其實跟Button是幾乎一樣的.

布局中:

<Switch 
    android:id="@+id/open" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textOff="藍牙關(guān)閉中" 
    android:textOn="藍牙開啟中" /> 


java代碼中

open.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
 
      @Override 
      public void onCheckedChanged(CompoundButton buttonView, 
          boolean isChecked) { 
        // TODO Auto-generated method stub 
        if (isChecked) { 
          mBluetoothAdapter.enable();//打開藍牙 
        } else { 
          mBluetoothAdapter.disable();// 關(guān)閉藍牙 
        } 
      } 
    }); 

就是這樣了,一看就明白了.

相關(guān)文章

最新評論

小金县| 林口县| 建始县| 哈巴河县| 曲阜市| 简阳市| 德江县| 册亨县| 贺州市| 全南县| 房山区| 晋城| 旬阳县| 威远县| 旌德县| 余姚市| 许昌县| 乐东| 云浮市| 远安县| 长治市| 渭南市| 岳西县| 沁源县| 孝昌县| 台北县| 濉溪县| 若尔盖县| 嘉荫县| 忻城县| 晋州市| 云霄县| 遵义市| 清苑县| 德州市| 宾阳县| 福海县| 东丰县| 潮安县| 中江县| 达州市|