星空网 > 软件开发 > 操作系统

我的android学习经历13

ToggleButton控件的使用

ToggleButton控件看名字就可以知道它是一个 “开关” 控件,也就是有两种不同状态的按钮。

主要的特别属性有三个:

android:textOn="开"              ----状态为true时,显示的文本
android:textOff="关"              ----状态为false时,显示的文本
android:checked="true"        ----标识状态

下面举一个简单的例子来说明ToggleButton的使用方法

需要两个控件,一个是ToggleButton,一个是TextView,实现的功能是用ToggleButton的不同状态来显示不同的文本(还可以实现更复杂的操作,请根据自己的情况编写)

布局文件代码为:

<ToggleButton
android:id="@+id/toggleButton1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textOn="开"
android:textOff="关"
android:checked="true"/>
<!-- android:checked="true"属性主要是显示是否被选中,如果没有写,则默认为未选中 -->

<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/toggleButton1"
android:layout_alignParentRight="true"
android:layout_below="@+id/toggleButton1"
android:layout_marginTop="31dp"
/>

 

源代码文件中的内容主要为:

private ToggleButton tgB;
private TextView tV;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tgB=(ToggleButton) findViewById(R.id.toggleButton1);
tV=(TextView) findViewById(R.id.textView1);

tgB.setOnCheckedChangeListener(new OnCheckedChangeListener() {


public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//根据ToggleButton控件的不同状态来显示不同的文本
if(isChecked){
tV.setText("此时状态为开");
tV.setTextSize(30f);
}else{
tV.setText("此时状态为关");
tV.setTextSize(30f);
}

}
});

}

界面为:

我的android学习经历13images/loading.gif' data-original="http://images2015.cnblogs.com/blog/927039/201604/927039-20160412091700191-217666900.png" />我的android学习经历13

 

 

有不对的地方还请指教,谢谢

 




原标题:我的android学习经历13

关键词:Android

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

越南双清专线:https://www.goluckyvip.com/tag/48339.html
科学选品:https://www.goluckyvip.com/tag/4834.html
越南外贸:https://www.goluckyvip.com/tag/48340.html
越南消费趋势:https://www.goluckyvip.com/tag/48342.html
越南专线货运:https://www.goluckyvip.com/tag/48343.html
越南专线运输:https://www.goluckyvip.com/tag/48344.html
九月初新疆旅游服装搭配(新疆游玩必备衣服清单):https://www.vstour.cn/a/408257.html
黄果树瀑布景区景点 - 黄果树瀑布景区景点分布图:https://www.vstour.cn/a/408258.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流