Android用户界面基础之ToggleButton学习
2016-01-23 00:08
344 查看
列表内容
ToggleButton,状态按钮控件。该控件扩展自CompoundButton类,具有选中和非选中两种状态。可以通过单击在两种状态间切换。主要的xml属性包括:android:textOn和android:textOff分别用于设置当按钮处于选中和非选中状态时显示的文本内容。可以通过isChecked()方法获取状态按钮的当前状态。
·状态按钮
继承自CompoundButton
·主要属性
Android:textOn
Android:textOf
checked:默认是否选中
·主要方法
isChecked();
·主要事件
setOnClickListener(onClickListener l)
实例:
效果图:
XML布局:
activity:
ToggleButton,状态按钮控件。该控件扩展自CompoundButton类,具有选中和非选中两种状态。可以通过单击在两种状态间切换。主要的xml属性包括:android:textOn和android:textOff分别用于设置当按钮处于选中和非选中状态时显示的文本内容。可以通过isChecked()方法获取状态按钮的当前状态。
·状态按钮
继承自CompoundButton
·主要属性
Android:textOn
Android:textOf
checked:默认是否选中
·主要方法
isChecked();
·主要事件
setOnClickListener(onClickListener l)
实例:
效果图:
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="horizontal" tools:context=".MainActivity" > <TextView android:id="@+id/tvInfo" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1.0" android:textSize="20sp" android:text="状态按钮被打开" android:textColor="#fff000aa" /> <ToggleButton android:id="@+id/tbText" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textOff="关闭" android:textOn="打开" android:checked="true" android:onClick="doClick"/> </LinearLayout>
activity:
package com.ecainiao.togglebutton; import android.os.Bundle; import android.app.Activity; import android.view.View; import android.widget.TextView; import android.widget.ToggleButton; public class MainActivity extends Activity { private TextView tvInfo; private ToggleButton tbText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tvInfo = (TextView) findViewById(R.id.tvInfo); tbText = (ToggleButton) findViewById(R.id.tbText); } public void doClick(View v){ if(tbText.isChecked()){ tvInfo.setText("状态按钮打开"); }else{ tvInfo.setText("状态按钮关闭"); } } }
相关文章推荐
- android根据ViewPager的滑动给ScroolView上设置当前tab的颜色
- 开源安卓Http文件下载框架file-downloader的使用
- Android 开发中常用 ADB 命令总结
- android:列表对话框(11)
- android:几种对话框分类(11)
- android中PopupWindow弹出式窗体菜单简单记录(11)
- android中popmenu简单记录(11)
- android:中两种创建Fragment的方法(12)
- android:选项(OptionMenu)和上下文菜单 (ContextMenu )简单记录(11
- android:ListFragment简单记录(12)
- Android Studio 2.0 preview3 BUG
- akira的android豆知识160122
- Android基础之自定义控件、布局以及ListView控件
- Android Radiobutton 文字颜色随选中状态而改变
- Ace教你一步一步做Android新闻客户端(五) 优化Listview
- Android keystore 的MD5、SHA1值获取方法
- Android ANR
- Android 内容提供器(Content Provider)介绍
- android studio 运行java文件
- Android Studio 2.0 Preview 6 尝鲜记