当前位置: 移动技术网 > IT编程>移动开发>Android > android基本控件ToggleButton&Switch使用指南

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

2019年07月24日  | 移动技术网IT编程  | 我要评论

同居牢友片尾曲,万惠事件,两个无线路由器怎么桥接

togglebutton(开关按钮)和switch(开关)讲解:

一、核心属性讲解:

(1)togglebutton

texton:按钮被选中的时候文字显示

textoff:按钮没有被选中的时候文字显示

togglebutton的状态只能是选中和未选中,并且需要为不同的状态设置不同的显示文本。

以下案例为togglebutton的用法

目录结构

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="关灯"
    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:设置texton/off的时候文字是否显示

android:showtext:设置on/off的时候是否显示文字,boolean

android:splittrack:是否设置一个间隙,让滑块与底部图片分隔,boolean

android:switchminwidth:设置开关的最小宽度

android:switchpadding:设置滑块内文字的间隔

android:textoff:按钮没有被选中时显示的文字

android:texton:按钮被选中时显示的文字

android:textstyle:文字风格,粗体,斜体写划线那些

android:track:底部的图片

android:thumb:滑块的图片

可以自己动手试一试每一个属性

在做一个蓝牙开关时候,用到了switch,记一下用法,其实跟button是几乎一样的.

布局中:

<switch 
    android:id="@+id/open" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textoff="蓝牙关闭中" 
    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();// 关闭蓝牙 
        } 
      } 
    }); 

就是这样了,一看就明白了.

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网