当前位置: 首页 > article >正文

Android UI 详解之ToggleButton按钮和Swith按钮

                               Android UI 详解之ToggleButton、Swith

1、ToggleButton 和Swith同样是继承Button而来的,所以剧本Button的一切属性


      ToggleButton新加的最重要属性,

         android:checked             设置按钮是否被选中

         android:textOff                设置当该按钮的状态关闭时显示的文本

         android:textOn                设置当该按钮的状态为打开时显示的文本

 

2、下面是自定义的ToggleButton

      设计思想,先选取两张图片,然后通过selector 去设置状态变化的对应图片

      在代码中通过setOnchangeCheckListener()去监听,注意:与单选和按钮的监听器不同,和复选的按钮监听器一样,上一次在讨论单选按钮的时候没后用到参数,这次我们用一下,其实参数也就是两个值True和False,和调用ischecked 一样,只不过这里用True和False更能易懂,因为他毕竟是开关,

效果图

代码如下

package com.example.togglebuttondemo;import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.ToggleButton;@SuppressLint("ShowToast")
public class MainActivity extends Activity {private ToggleButton tbb;private Button bt;private EditText et;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);tbb = (ToggleButton)findViewById(R.id.tb1);et = (EditText)findViewById(R.id.et);tbb.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton arg0, boolean isChecked) {Log.i("Toggle","Toggle1");if (tbb.isChecked()){et.setText("Yes");Log.i("Toggle","Yes");}else{et.setText("NO");Log.i("Toggle","NO");}}});}}


布局代码

<?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"><ToggleButton android:id="@+id/tb1"android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true"android:textOn="1"android:textOff="2"android:background="@drawable/btn_toggle"/><EditText android:id="@+id/et"android:layout_width="match_parent"android:layout_height="50dp"android:background="@drawable/et"/>
</LinearLayout>


btn_toggle.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_checked="false" android:drawable="@drawable/btn_toggle_no" /><item android:state_checked="true" android:drawable="@drawable/btn_toggle_yes" />
</selector>


3、Swith

代码

package org.crazyit.ui;import android.app.Activity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.ToggleButton;public class ToggleButtonTest extends Activity
{ToggleButton toggle;Switch switcher;@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);switcher = (Switch)findViewById(R.id.switcher);final LinearLayout test = (LinearLayout)findViewById(R.id.test);OnCheckedChangeListener listener = new OnCheckedChangeListener(){@Overridepublic void onCheckedChanged(CompoundButton button, boolean isChecked){if(isChecked){//设置LinearLayout垂直布局test.setOrientation(1);}else{//设置LinearLayout水平布局test.setOrientation(0);}}};switcher.setOnCheckedChangeListener(listener);}
}

layout

<?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"><Switch android:id="@+id/switcher"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textOff="横向排列"android:textOn="纵向排列"android:thumb="@drawable/check"android:checked="true"/>
<!-- 定义一个可以动态改变方向的线性布局 -->
<LinearLayout android:id="@+id/test"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent">
<Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="测试按钮一"/>
<Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="测试按钮二"/>
<Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="测试按钮三"/>
</LinearLayout>
</LinearLayout>
效果图


http://www.lryc.cn/news/2414679.html

相关文章:

  • 怎么在桌面上嵌入窗口(使用FindWindow(),FindWindowEx(),EnumChildWindows())
  • ADODB用法详解
  • 去掉字符串首尾逗号_去除字符串首尾空格和特殊字符
  • C/C++数据结构课程设计(15题)[2023-10-29]
  • 致敬mentohust,路由器使用Socket认证华科校园网
  • Python 中如何使用 @property
  • 使用TerminateProcess结束进程时,错误码为5的解决方法
  • 常量指针与指针常量的区别
  • 信道估计(channel estimation)图解——从SISO到MIMO原理介绍
  • tbody标签的作用介绍
  • CreateEvent的理解
  • 短剧小程序源码|小程序短剧平台源码搭建
  • background属性介绍
  • [翻译] LaTeX 中的列表
  • 内存地址的计算方法
  • ROS2 学习笔记(一)新建项目的基础流程
  • 分享个好用的开源录屏工具 Captura
  • 高速收发器之发送器详解(Transmitter)
  • 计算机程序设计艺术 介绍
  • CreateMutex创建互斥内核对象
  • 目前最受欢迎的12个Python web框架,你用过几个?
  • SqlCommand.ExecuteNonQuery()方法的使用注意
  • Delicatessen音乐名词什么意思
  • Tribon二次开发-COM接口的使用(抽取数据)
  • 全球免费公共 DNS 解析服务器 IP 地址列表推荐 (解决无法上网/加速/防劫持)
  • dubbo和zookeeper
  • Bloom filter 过滤(布隆过滤算法)原理
  • C语言实现集合的基本运算
  • 洛谷4438 [Hnoi2018]道路 【树形dp】
  • 多质量动态系统仿真(Matlab代码实现)