Android中ToggleButton开关状态按钮控件使用方法详解

Android中ToggleButton开关状态按钮控件使用方法详解,第1张

概述ToggleButton开关状态按钮控件使用方法,具体内容如下一、简介1、2、ToggleButton类结构

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状态开关

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android中ToggleButton开关状态按钮控件使用方法详解全部内容,希望文章能够帮你解决Android中ToggleButton开关状态按钮控件使用方法详解所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1144877.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存