您的位置:首页 > 移动开发 > Android开发

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: