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

ToggleButton(开关按钮)和Switch(开关)的使用

2017-04-15 18:17 701 查看
ToggleButton属性:

android:disabledAlpha:设置按钮在禁用时的透明度

android:textOff:按钮没有被选中时显示的文字

android:textOn:按钮被选中时显示的文字 另外,除了这个我们还可以自己写个selector,然后设置下Background属性即可~

Switch属性:

android:showText:设置on/off的时候是否显示文字,boolean

android:splitTrack:是否设置一个间隙,让滑块与底部图片分隔,boolean

android:switchMinWidth:设置开关的最小宽度

android:switchPadding:设置滑块内文字的间隔

android:switchTextAppearance:设置开关的文字外观,暂时没发现有什么用...

android:textOff:按钮没有被选中时显示的文字

android:textOn:按钮被选中时显示的文字

android:textStyle:文字风格,粗体,斜体写划线那些

android:track:底部的图片

android:thumb:滑块的图片

android:typeface:设置字体,默认支持这三种:sans, serif, monospace;除此以外还可以使用 其他字体文件(*.ttf),首先要将字体文件保存在assets/fonts/目录下,不过需要在Java代码中设置: Typeface
typeFace =Typeface.createFromAsset(getAssets(),"fonts/HandmadeTypewriter.ttf"); textView.setTypeface(typeFace);

使用实例

控件:
<ToggleButton
android:id="@+id/tbtn_open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:textOff="关闭声音"
android:textOn="打开声音" />
设置监听事件:

tbtn_open = (ToggleButton) findViewById(R.id.tbtn_open);
swh_status = (Switch) findViewById(R.id.swh_status);
tbtn_open.setOnCheckedChangeListener(this);编写函数

case R.id.tbtn_open:
if(compoundButton.isChecked()) Toast.makeText(this,"打开声音",Toast.LENGTH_SHORT).show();
else Toast.makeText(this,"关闭声音",Toast.LENGTH_SHORT).show();
break;

效果:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android switch 编程