android-UI组件实例大全(五)------开关按钮ToggleButton和开关Switch
2015-12-04 19:02
555 查看
这两个组件的话因为比较简单,某些书都会直接略过,不过笔者觉得还是有比较介绍一下这两个组件:\
相信有朋友都不知道这两个组件是怎么样的,so,普及下,上图:
ToggleButton:开关按钮:
,就是点击以后那个蓝色的底部会变暗,就是一个开关,蓝色的时候表示on,灰色off
Switch:开关:
点击以后会左右移动,在右边的时候表示on,左边表示off
!!!这里要注意一下,switch是4.0以后才出现的,所以要修改一下androidmanifest.xml中的最低版本的minsdk 改为 14或以上,不然会报错
因为这两个都是Button的子类,所以继承了Button的大部分属性,这里的话只将特殊的属性
话不多说
代码:
main.xml文件:
[html] view
plaincopyprint?
<!-- 定义一个ToggleButton按钮 -->
<ToggleButton
android:id="@+id/TogBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="声音开"
android:textOff="声音关闭"
android:checked="true"
/>
<!-- 定义一个switch开关 -->
<Switch
android:id="@+id/switcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="横向排列"
android:textOff="纵向排列"
android:thumb="@drawable/btnon"
android:checked="true"
/>
<Button
android:id="@+id/btntest1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试按钮1"
/>
<Button
android:id="@+id/btntest2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试按钮2"
/>
<Button
android:id="@+id/btntest3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试按钮3"
/>
MainActivity.java部分:
[java] view
plaincopyprint?
private ToggleButton toggle;
private Switch switcher;
private LinearLayout linear;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toggle = (ToggleButton) findViewById(R.id.TogBtn);
switcher = (Switch) findViewById(R.id.switcher);
linear = (LinearLayout) findViewById(R.id.LinearLayout1);
switcher.setOnCheckedChangeListener(listener);
}
private OnCheckedChangeListener listener = new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)linear.setOrientation(0);
else linear.setOrientation(1);
}
};
运行截图:
代码解释:
在xml文件中分别定义了一个ToggleButton和Switch开关,设置了textoff和textOn时显示的文本,设置选择状态,并设置了三个普通按钮
在java主文件中实例化两个开关对象,findViewByid后,再new一个onCheckedChangeListener对象,为switch添加触发事件;
setOrientation():指定linearlayout的方向,0和1,默认是horizontal横向排列,即0的时候!1的话就是竖直方向vertical
相信有朋友都不知道这两个组件是怎么样的,so,普及下,上图:
ToggleButton:开关按钮:
,就是点击以后那个蓝色的底部会变暗,就是一个开关,蓝色的时候表示on,灰色off
Switch:开关:
点击以后会左右移动,在右边的时候表示on,左边表示off
!!!这里要注意一下,switch是4.0以后才出现的,所以要修改一下androidmanifest.xml中的最低版本的minsdk 改为 14或以上,不然会报错
因为这两个都是Button的子类,所以继承了Button的大部分属性,这里的话只将特殊的属性
话不多说
代码:
main.xml文件:
[html] view
plaincopyprint?
<!-- 定义一个ToggleButton按钮 -->
<ToggleButton
android:id="@+id/TogBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="声音开"
android:textOff="声音关闭"
android:checked="true"
/>
<!-- 定义一个switch开关 -->
<Switch
android:id="@+id/switcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="横向排列"
android:textOff="纵向排列"
android:thumb="@drawable/btnon"
android:checked="true"
/>
<Button
android:id="@+id/btntest1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试按钮1"
/>
<Button
android:id="@+id/btntest2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试按钮2"
/>
<Button
android:id="@+id/btntest3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试按钮3"
/>
MainActivity.java部分:
[java] view
plaincopyprint?
private ToggleButton toggle;
private Switch switcher;
private LinearLayout linear;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toggle = (ToggleButton) findViewById(R.id.TogBtn);
switcher = (Switch) findViewById(R.id.switcher);
linear = (LinearLayout) findViewById(R.id.LinearLayout1);
switcher.setOnCheckedChangeListener(listener);
}
private OnCheckedChangeListener listener = new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)linear.setOrientation(0);
else linear.setOrientation(1);
}
};
运行截图:
代码解释:
在xml文件中分别定义了一个ToggleButton和Switch开关,设置了textoff和textOn时显示的文本,设置选择状态,并设置了三个普通按钮
在java主文件中实例化两个开关对象,findViewByid后,再new一个onCheckedChangeListener对象,为switch添加触发事件;
setOrientation():指定linearlayout的方向,0和1,默认是horizontal横向排列,即0的时候!1的话就是竖直方向vertical
相关文章推荐
- android-UI组件实例大全(四)------单选按钮(RadioButton)和复选框(CheckBox)
- android-UI组件实例大全(三)------按钮Button与图片按钮ImageButton
- easyui使用,导入js顺序
- android-UI组件实例大全(二)------EditText编辑框
- android UI组件属性------inputType
- android-UI组件实例大全(一)------TextView文本框
- JDBC的autoCommit为true时,其事务管理测试
- 产生制定位数的随机字符串,以及StringBuffer,StringBuilder区别
- hdu1159 Common Subsequence
- 4.ROS在Pcduino上的安装
- 数据库主键设计-用GUID还是Int(int32,int64)做主键(primaryKey)
- 对xib构建的UITableViewCell的重用方法和代码添加约束的方法 学习笔记
- hdu 5493 Queue treap实现将元素快速插入到第i个位置
- UI第十七天:图⽚异步下载、KVO
- remoteSort =true 用在store; 和sortable:true 用在column里的一个字段上
- iOS 新特性分列式 之 iOS 6.x - 主要内容:自动布局、集合视图、UI 状态保持
- 在iOS 8中使用UIAlertController
- HDUOJ Number Sequence找规律
- 杭电-5504GT and sequence
- poj 3239 Solution to the n Queens Puzzle n皇后问题的构造解法