widgets之switch和ToggleButton
来源:互联网 发布:律师知乎 编辑:程序博客网 时间:2024/06/05 04:48
1.switch是一种只有两种状态的控件如:
继承关系如下:java.lang.Object
↳Android.view.View
↳android.widget.TextView
↳android.widget.Button
↳android.widget.CompoundButton
↳android.widget.Switch
使用方法和其他控件差不多:
<Switch android:id="@+id/switch01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOff="关闭" android:textOn="开启" />
监听器和checkbox和RadioButton一样也是CompoundButton.OnCheckedChangeListener(CompoundButton arg0, boolean arg1)
第一个参数不用多解释就是这个Switch控件,第二参数指的是现在处于哪一种状态,如果是true则处于textOn,false处于textOff。
sw.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton arg0, boolean arg1) {// TODO Auto-generated method stubif(arg1){Toast.makeText(MainActivity.this, "你选中了打开", 3333).show();}else{Toast.makeText(MainActivity.this, "你选关闭", 3333).show();}}});
2.ToggleButton和Switch一样也是一个开关组件,也是只有两个状态量,效果如下:
注意开和关的区别,它也可以动态设置文字。如下:
<ToggleButton android:id="@+id/tgb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:textOn="@string/open" android:textOff="@string/close" />
监听器也和Switch一样如下:
tg.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton arg0, boolean arg1) {// TODO Auto-generated method stubif(arg1){Toast.makeText(MainActivity.this, "打开", 3333).show();}else{Toast.makeText(MainActivity.this, "关闭", 3333).show();}}});这两个开关组件的功能是一样的只不过只是展现的形式不同而已。
0 0
- widgets之switch和ToggleButton
- Android ToggleButton和Switch
- ToggleButton和Switch
- Android UI之选择器-ToggleButton、Switch、CheckBox和RadioButton
- Android学习路之开关按钮ToggleButton和开关Switch
- Android控制之ToggleButton和Switch与RatingBar
- Android控件之ToggleButton、Switch
- Android控件ToggleButton和Switch
- 开关控件Switch和ToggleButton
- ToggleButton和Switch改变布局
- ToggleButton和Switch的用法
- Android Switch和ToggleButton类
- Android UI控件之ToggleButton、Switch
- 用户界面View之Switch与ToggleButton
- CompoundButton之RadioButton、CheckBox、ToggleButton、Switch
- Android学习之RatingBar,SeekBar,ToggleButton,Switch
- 004 RadioButton,ToggleButton和Switch控件
- Android—ToggleButton、Switch和RatingBar使用
- sql大于8000字符的字符替换
- intellij idea 集成tfs(teem foundation server)
- Linux中断处理驱动程序编写
- iOS学习笔记:Object-C基础语法(OC介绍与Foundation初步)
- 重置QComboBox项的时候产生currentIndexChanged信号
- widgets之switch和ToggleButton
- c++ 队列Queue的实现 - 练习题
- poj3270cow sorting 置换群裸题
- stl-thread-001
- Lua_第 21 章 操作系统库
- .NET执行sql文件
- Dev之GridControl详解
- codeforces_598D. Igor In the Museum(dfs)
- leetcode——9——Palindrome Number