界面编程之基本界面组件(5)ToggleButton(状态开关按钮)
来源:互联网 发布:智能网络机顶盒 编辑:程序博客网 时间:2024/05/16 23:42
ToggleButton用于切换程序中的某种状态。
ToggleButton有三个XML属性:android:checked,设置该按钮是否被选中,对应的方法为setChecked(boolean);
android:textOff,设置当按钮没有被选中时显示的文本;
android:textOn,设置当按钮没有被选中时显示的文本。
下面的例子示范动态控制布局的例子,随着按钮状态的改变,界面布局在水平和垂直布局之间切换。
XML源代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".ToggleButton" > <ToggleButton android:id="@+id/togglebutton" android:layout_width="match_parent" android:layout_height="wrap_content" android:checked="true" android:textOff="横向排列" android:textOn="纵向排列" /> <LinearLayout android:id="@+id/layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 1" android:textSize="11pt" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 1" android:textSize="11pt" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 1" android:textSize="11pt" /> </LinearLayout></LinearLayout>
Java源代码:
package com.example.togglebutton;import android.os.Bundle;import android.app.Activity;import android.view.*;import android.widget.CompoundButton;import android.widget.CompoundButton.OnCheckedChangeListener;import android.widget.LinearLayout;import android.widget.ToggleButton;public class ToggleButtonTest extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_toggle_button);ToggleButton toggleButton = (ToggleButton) findViewById(R.id.togglebutton);final LinearLayout layout = (LinearLayout) findViewById(R.id.layout);toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton arg0, boolean arg1) {// TODO Auto-generated method stubif (arg1)layout.setOrientation(1);elselayout.setOrientation(0);}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.activity_toggle_button, menu);return true;}}
- 界面编程之基本界面组件(5)ToggleButton(状态开关按钮)
- UI控件之开关按钮(ToggleButton与Switch)
- 界面编程之基本界面组件(6)AnalogClock 和 DigitalClock(时钟)
- 界面编程之基本界面组件(7)ImageView(图像视图)
- 状态开关按钮(ToggleButton)与 开关(Switch)的功能与用法
- 第13章:状态开关按钮(ToggleButton)和开关(Switch)的功能和用法
- 状态开关按钮(ToggleButton)和开关(Switch)的功能与用法
- 状态开关按钮(ToggleButton)和开关(Switch)的功能与用法
- UI控件之ToggleButton(开关按钮)和Switch(开关)
- 【9】ToggleButton(开关按钮)和Switch(开关)
- 安卓界面基本组件---按钮
- Android ToggleButton(开关)
- Android自学笔记之ToggleButton(开关按钮)的功能、特殊属性、用法
- togglebutton的使用(开关按钮)改变布局的方式
- 【Android 开发】:UI控件之开关状态按钮 ToggleButton 的使用方法
- 开关按钮ToggleButton
- ToggleButton开关按钮
- ToggleButton开关按钮使用
- shell——tr的用法
- 国外源代码搜索
- c语言的奇葩书写,不断总结
- bootstrps 、bagging与 boosting
- BigDecimal
- 界面编程之基本界面组件(5)ToggleButton(状态开关按钮)
- 制作linux系统的grub启动光盘
- 基础编程学习笔记二(makefile工程管理)
- Fancybox的API和配置选项说明
- 1318我爱学java
- 四巨头GoF的《设计模式》中23种设计模式的UML图 - 1 of 4
- fn contains 包含字符串 判断
- 数据库学习笔记(一)
- [计算机] -32768~32767 计算机中的 -1 表示