Android-ToggleButton&CheckTextView&CheckBox
2015-06-08 21:18
501 查看
Android-ToggleButton&CheckTextView&CheckBox
一 定义
ToggleButton:单个选择框
就是类似于选择开关的按钮
CheckBox:复选框
与ToggleButton功能类似,实现方法类似
CheckTextView:点击文本后选中,
与CheckBox功能类似,时间机制也相同
二 使用方法
1 xml文件中定义ToggleButton控件,设置默认的属性,例如textOn和textOff
<ToggleButton
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView"
android:layout_below="@+id/button3"
android:textOn="close"
android:textOff="open"
android:background="@drawable/button5"
android:text="ToggleButton" />
2 源文件中调用,和设置监听:mtoggleButton.setOnCheckedChangeListener
三 自定义实现
1 代码实现:
private void showToggleButton()
{
mtoggleButton = (ToggleButton)findViewById(R.id.toggleButton1);
mtoggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1)
{
// TODO Auto-generated method stub
if (arg1)
{
mtoggleButton.setBackgroundResource(R.drawable.button5);//改变按钮的背景为button5
Log.i("chengzhi log", "open");
}
else
{
mtoggleButton.setBackgroundResource(R.drawable.button6)//改变按钮的背景为button6
Log.i("chengzhi log", "close");
}
}
});
2 xml文件实现:
<item android:state_checked="true" android:drawable="@drawable/button5"></item>开关为true,改变按钮的背景为button5
<item android:state_checked="false" android:drawable="@drawable/button6"></item>开关为false,改变按钮的背景为button6
一 定义
ToggleButton:单个选择框
就是类似于选择开关的按钮
CheckBox:复选框
与ToggleButton功能类似,实现方法类似
CheckTextView:点击文本后选中,
与CheckBox功能类似,时间机制也相同
二 使用方法
1 xml文件中定义ToggleButton控件,设置默认的属性,例如textOn和textOff
<ToggleButton
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView"
android:layout_below="@+id/button3"
android:textOn="close"
android:textOff="open"
android:background="@drawable/button5"
android:text="ToggleButton" />
2 源文件中调用,和设置监听:mtoggleButton.setOnCheckedChangeListener
三 自定义实现
1 代码实现:
private void showToggleButton()
{
mtoggleButton = (ToggleButton)findViewById(R.id.toggleButton1);
mtoggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1)
{
// TODO Auto-generated method stub
if (arg1)
{
mtoggleButton.setBackgroundResource(R.drawable.button5);//改变按钮的背景为button5
Log.i("chengzhi log", "open");
}
else
{
mtoggleButton.setBackgroundResource(R.drawable.button6)//改变按钮的背景为button6
Log.i("chengzhi log", "close");
}
}
});
2 xml文件实现:
<item android:state_checked="true" android:drawable="@drawable/button5"></item>开关为true,改变按钮的背景为button5
<item android:state_checked="false" android:drawable="@drawable/button6"></item>开关为false,改变按钮的背景为button6
相关文章推荐
- Android-Button
- Android屏幕适配全攻略(最权威的官方适配指导)
- SVN在Android Studio中的配置
- android的线性布局
- Android Service
- Android-TextView
- android常用开发框架
- android studio 安装教程——基于1.2.1.1
- Android应用Activity、Dialog、PopWindow、Toast窗口添加机制及源码分析
- 【Android笔记】MediaPlayer基本用法
- [Android分享] ViewPager的setOnPageChangeListener方法详解
- Android - LayoutInflater
- android.os.NetworkOnMainThreadException
- Android中Toast不显示的解决方案
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid
- Android中Cursor类的概念和用法
- Android Studio 常用设置说明
- ant 打包android应用
- 安卓开发黄金搭档:android-studio+Genymotion模拟器
- Android 5.0 documentation CHM 版本 下载