开关按钮(ToggleButton&Switch)
开关按钮,很实用的小东西。
下面上实例:
-------------------------------我是邪恶的分割线---------------------------------------
1.java代码
package gdp.switchtestv1; import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.ToggleButton; public class MainActivity extends Activity {
private ToggleButton toggle;
private Switch switcher;
private LinearLayout test;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); //获取空间对象
toggle=(ToggleButton)findViewById(R.id.toggle);
switcher=(Switch)findViewById(R.id.switcher);
test=(LinearLayout)findViewById(R.id.test); //绑定监听器
toggle.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
test.setOrientation(1);
}else{
test.setOrientation(0);
}
}
}); switcher.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
test.setOrientation(1);
}else{
test.setOrientation(0);
}
}
});
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }
2.xml文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- texton表示开关打开状态显示的文本,textoff表示开关关闭状态显示的文本 -->
<!-- checked属性为默认开关所处的状态 -->
<ToggleButton
android:id="@+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="on"
android:textOff="off"
android:checked="true"
/>
<Switch
android:id="@+id/switcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="on"
android:textOff="off"
android:checked="true"
/>
<LinearLayout
android:id="@+id/test"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="bn1"
/>
<Button
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="bn2"
/>
<Button
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="bn3"
/>
</LinearLayout> </LinearLayout>
开关按钮(ToggleButton&Switch)的更多相关文章
- Android学习笔记-开关按钮ToggleButton和开关Switch
本节给大家介绍的Android基本UI控件是:开关按钮ToggleButton和开关Switch,这两个其实都是开关组件,只是后者需要在Android 4.0以后才能使用 所以AndroidManif ...
- Android——滚动视图(ScrollView)图片视图(ImageView)、状态开关按钮(ToggleButton)、时钟
xml <?xml version="1.0" encoding="utf-8"?> <!--滚动视图--> <ScrollVie ...
- android基本控件学习-----ToggleButton&Switch
ToggleButton(开关按钮)和Switch(开关)讲解: 一.核心属性讲解: (1)ToggleButton textOn:按钮被选中的时候文字显示 textOff:按钮没有被选中的时候文字显 ...
- 状态开关按钮(ToggleButton)和开关(Switch)
ToggleButton支持的XML属性及相关方法1.android:checked----->setChecked(boolean) ----->设置该按钮是否被选中2.android: ...
- iOS的settings bundle中开关按钮(Toggle Switch)取不到值的问题
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 在Xcode7.2中设置App的settings bundle ...
- Android——图片视图(ImageView)、状态开关按钮(ToggleButton)、时钟、图片透明度、滚动和时间选择器
activity_ui1.xml dth="wrap_content" android:layout_height="wrap_content" android ...
- PyQt4开关按钮ToggleButton
PyQt4没有开关按钮部件.但是我们可以使用在特殊状态下的QPushButton部件来创建开关按钮.而所谓的开关按钮就是一个具有按下和未按下两种状态的普通赶牛.用户可以通过单击按钮来切换其开或者关的状 ...
- ToggleButton与Switch
状态开关按钮togglebutton和开关switch 状态开关按钮togglebutton和开关switch是由button派生出来的,本质也是按钮,支持BUtton的各种属性,从功能上看,Togg ...
- Android ToggleButton Example--开关按钮
Android ToggleButton Example 在 Android 中, “android.widget.ToggleButton” 是个特殊的类,可以渲染出一个“开关按钮” ,顾名思义, ...
随机推荐
- bzoj 4455 [Zjoi2016]小星星 树形dp&容斥
4455: [Zjoi2016]小星星 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 643 Solved: 391[Submit][Status] ...
- 获得NOTEPAD++ Download Manager的所有下载列表的内容的au3脚本
;~ 获得NOTEPAD++ Download Manager的所有下载列表的内容的au3脚本 ;~ 作者: 鹏程万里 ;~ Email:aprial@163.com ;~ 创建日期: 2014年11 ...
- python enumerate元素的时候可以获取下标,并且可以指定开始的下标值。
list=["a","b","c","d","e"] for i,item in enumerate ...
- Linux Oracle DB log 清理
sid_list=`/bin/ps -ef |/bin/grep smon |/bin/grep -v grep | /bin/cut -f3 -d_` host_name=`hostname` cd ...
- MVC5的坑
事情是这样的,今天在写一个功能模块的时候,创建的方法,到controller里,死活为null 以前从没出现这种情况啊,但是区别是这个代码是多层跳转进来的,难道是页面跳转太多,还记得之前的model, ...
- Python的扩展接口[2] -> 动态链接库DLL[0] -> 动态链接库及辅助工具
动态链接库 / Dynamic Link Library 目录 动态链接库简介 函数封装DLL 组件对象模型COM 如何判断.dll文件是COM还是DLL 辅助工具 1 动态链接库简介 / DLL I ...
- vue的路由设置小结
vue的路由设置小结 // 异步路由的编写示例.其中针对component字段进行懒加载及分块处理,提升首屏加载速度的同时,也可以手动控制让某些页面合并到一个单独的js文件中,而不是每个页面都是一个j ...
- JSON 中的 key
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.易于人阅读和编写.同时也易于机器解析和生成.它基于JavaScript(Standard ECMA-262 ...
- Paint Fence -- LeetCode
There is a fence with n posts, each post can be painted with one of the k colors. You have to paint ...
- [COCI2017-2018 Contest5] Birokracija
题目描述 Mirko has become CEO of a huge corporation. This corporation consists of N people, labeled fro ...