免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1922 | 回复: 2
打印 上一主题 下一主题

[Android] 状态开关按钮(ToggleButton)和开关(Switch) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-07-24 14:12 |只看该作者 |倒序浏览
ToggleButton支持的XML属性及相关方法
1.android:checked----->setChecked(boolean)
----->设置该按钮是否被选中
2.android:textOff----->设置当该按钮的状态关闭时显示的文本
3.android:textON----->设置当该按钮的状态打开时显示的文本




Switch支持的XML属性及相关方法
1.android:checked----->setChecked(boolean)
----->设置该按钮是否被选中
2.android:textOff----->设置当该按钮的状态关闭时显示的文本
3.android:textON----->设置当该按钮的状态打开时显示的文本
4.android:switchMinWidth----->setSwitchMinWidth(int)
----->设置该开关的最小宽度
5.android:switchPadding----->setSwitchMinWidth(int)
----->设置该开关与标题文本之间的空白
6.android:switchTextAppearance----->setSwitchTextAppearance(Context,int)
----->设置开关图标上的文本样式
7.android:textStyle----->setSwitchTypeface(TYpeface)
----->设置开关的文本风格
8.android:thumb----->setThumbResource(int)
----->指定使用自定义Drawable绘制该开关的开关按钮
9.android:track----->setTrackResource(int)
----->指定使用自定义Drawable绘制该开关的轨道
10.android:typeface----->setSwitchTypeface(TYpeface)
-----设置该开关的文本的字体风格

Demo2\togglebutton_demo\src\main\res\layout\activity_main.xml
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:orientation="vertical"
  6.     tools:context=".MainActivity">

  7.     <!--定义一个ToggleButton按钮-->
  8.     <ToggleButton
  9.         android:id="@+id/toggle"
  10.         android:layout_width="wrap_content"
  11.         android:layout_height="wrap_content"
  12.         android:checked="true"
  13.         android:textOff="横向排列"
  14.         android:textOn="纵向排列" />
  15.     <!--定义一个可以动态改变方向的线性布局-->
  16.     <LinearLayout
  17.         android:id="@+id/test"
  18.         android:layout_width="match_parent"
  19.         android:layout_height="match_parent"
  20.         android:orientation="vertical">

  21.         <Button
  22.             android:layout_width="wrap_content"
  23.             android:layout_height="wrap_content"
  24.             android:text="第一个按钮"/>
  25.         <Button
  26.             android:layout_width="wrap_content"
  27.             android:layout_height="wrap_content"
  28.             android:text="第二个按钮"/>
  29.         <Button
  30.             android:layout_width="wrap_content"
  31.             android:layout_height="wrap_content"
  32.             android:text="第三个按钮"/>
  33.     </LinearLayout>

  34. </LinearLayout>
复制代码
Demo2\togglebutton_demo\src\main\java\com\ly\togglebutton_demo\MainActivity.java
  1. import android.app.Activity;
  2. import android.os.Bundle;
  3. import android.widget.CompoundButton;
  4. import android.widget.LinearLayout;
  5. import android.widget.ToggleButton;

  6. public class MainActivity extends Activity {
  7.     private ToggleButton toggle ;
  8.     private LinearLayout linear ;
  9.     @Override
  10.     protected void onCreate(Bundle savedInstanceState) {
  11.         super.onCreate(savedInstanceState);
  12.         setContentView(R.layout.activity_main);
  13.         toggle = (ToggleButton) findViewById(R.id.toggle);
  14.         linear = (LinearLayout) findViewById(R.id.test);
  15.         toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  16.             @Override
  17.             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  18.                 if (isChecked){
  19.                     //如果选中了,就设置垂直布局
  20.                     linear.setOrientation(LinearLayout.VERTICAL);
  21.                 }else {
  22.                     //否则就水平
  23.                     linear.setOrientation(LinearLayout.HORIZONTAL);
  24.                 }
  25.             }
  26.         });
  27.     }


  28. }
复制代码

论坛徽章:
80
20周年集字徽章-庆
日期:2020-10-28 14:09:1215-16赛季CBA联赛之北京
日期:2020-10-28 13:32:5315-16赛季CBA联赛之北控
日期:2020-10-28 13:32:4815-16赛季CBA联赛之天津
日期:2020-10-28 13:13:35黑曼巴
日期:2020-10-28 12:29:1520周年集字徽章-周	
日期:2020-10-31 15:10:0720周年集字徽章-20	
日期:2020-10-31 15:10:07ChinaUnix元老
日期:2015-09-29 11:56:3020周年集字徽章-年
日期:2020-10-28 14:14:56
2 [报告]
发表于 2015-07-24 14:55 |只看该作者
楼主这个什么意思?

论坛徽章:
59
2015七夕节徽章
日期:2015-08-24 11:17:25ChinaUnix专家徽章
日期:2015-07-20 09:19:30每周论坛发贴之星
日期:2015-07-20 09:19:42ChinaUnix元老
日期:2015-07-20 11:04:38荣誉版主
日期:2015-07-20 11:05:19巳蛇
日期:2015-07-20 11:05:26CU十二周年纪念徽章
日期:2015-07-20 11:05:27IT运维版块每日发帖之星
日期:2015-07-20 11:05:34操作系统版块每日发帖之星
日期:2015-07-20 11:05:36程序设计版块每日发帖之星
日期:2015-07-20 11:05:40数据库技术版块每日发帖之星
日期:2015-07-20 11:05:432015年辞旧岁徽章
日期:2015-07-20 11:05:44
3 [报告]
发表于 2015-08-11 15:40 |只看该作者
是在说几种按钮状态。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP