ToggleButton
来源:互联网 发布:js判断app安卓版本号 编辑:程序博客网 时间:2024/05/29 13:26
ToggleButton(开关按钮)是Android系统中比较简单的一个组件,是一个具有选中和未选择状态双状态的按钮,并且需要为不同的状态设置不同的显示文本。
第一个例子是通过Toast显示ToggleButton不同的状态时的信息
MainActivity.java
- package com.android.togglebutton;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Toast;
- import android.widget.ToggleButton;
- public class MainActivity extends Activity {
- //声明ToggleButton
- private ToggleButton togglebutton;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- togglebutton = (ToggleButton) findViewById(R.id.togglebutton);
- togglebutton.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- // 当按钮第一次被点击时候响应的事件
- if (togglebutton.isChecked()) {
- Toast.makeText(MainActivity.this, "你喜欢球类运动", Toast.LENGTH_SHORT).show();
- }
- // 当按钮再次被点击时候响应的事件
- else {
- Toast.makeText(MainActivity.this, "你不喜欢球类运动", Toast.LENGTH_SHORT).show();
- }
- }
- });
- }
- }
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"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- />
- <ToggleButton
- android:id="@+id/togglebutton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textOn="喜欢"
- android:textOff="不喜欢"
- />
- </LinearLayout>
strings.xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">你喜不喜欢球类运动?</string>
- <string name="app_name">测试ToggleButton</string>
- </resources>
效果图:
第二个例子通过图片的变化显示ToggleButton不同的状态时的图片
MainActivity.java
- package com.android.togglebutton;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.CompoundButton;
- import android.widget.CompoundButton.OnCheckedChangeListener;
- import android.widget.ImageView;
- import android.widget.ToggleButton;
- public class MainActivity extends Activity {
- //声明ImageView,ToggleButton
- private ImageView imageView;
- private ToggleButton toggleButton;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //通过findViewById获得ImageView,ToggleButton
- 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.pic_on:R.drawable.pic_off);
- }
- });
- }
- }
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/pic_off"
- android:layout_gravity="center_horizontal"
- />
- <ToggleButton
- android:id="@+id/toggleButton"
- android:layout_width="130dip"
- android:layout_height="wrap_content"
- android:textOn="开灯"
- android:textOff="关灯"
- android:layout_gravity="center_horizontal"
- />
- </LinearLayout>
效果图:
ToggleButton常用的XML属性
属性名称
描述
android:disabledAlpha
设置按钮在禁用时透明度。
android:textOff
未选中时按钮的文本
android:textOn
选中时按钮的文本
0 0
- ToggleButton
- togglebutton
- ToggleButton
- ToggleButton
- ToggleButton
- ToggleButton
- ToggleButton
- ToggleButton
- ToggleButton
- ToggleButton
- ToggleButton
- ToggleButton
- ToggleButton
- togglebutton
- ToggleButton
- ToggleButton
- ToggleButton
- Button,ToggleButton
- android中Scrollview 嵌套ListView显示不完整
- 希尔排序
- Dubbo简介及实例
- C# VS 2010 创建、安装、调试 windows服务(windows service)
- apache的https访问配置
- ToggleButton
- HDU-1102 Constructing Roads(最小生成树[Prim])
- Android笔记(二)LogCat
- cocos2d-x 3.7 C++ 使用lua做脚本
- Android自定义布局
- 阅读方法
- 求一个整数的所有位相加之和
- 黑马程序员---java基础之集合Set
- Redis在windows下的安装使用