当前位置: 移动技术网 > IT编程>移动开发>Android > Android开发之开关按钮用法示例

Android开发之开关按钮用法示例

2019年07月23日  | 移动技术网IT编程  | 我要评论

2014云南省公务员考试报名入口,游行,男生怎么穿衣打扮

本文实例讲述了android开发之开关按钮用法。分享给大家供大家参考,具体如下:

效果如下:

以下是布局文件:

<?xml version="1.0" encoding="utf-8"?>
<linearlayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/root"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">
  <!--定义一个togglebutton按钮-->
  <togglebutton
    android:id="@+id/toggle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textoff="横向排列"
    android:texton="纵向排列"
    android:checked="true"/>
  <switch
    android:id="@+id/switcher"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textoff="横向排列"
    android:texton="纵向排列"
    android:thumb="@drawable/thumb"
    android:checked="true"/>
  <!--定义一个可以动态改变方向的线性布局-->
  <linearlayout
    android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <button
      android:id="@+id/button01"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
    <button
      android:id="@+id/button02"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
    <button
      android:id="@+id/button03"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
  </linearlayout>
</linearlayout>

活动代码实现:

public class home extends appcompatactivity {
  togglebutton toggle ;
  switch switcher ;
  public void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);//显示manlayout
    toggle = (togglebutton) findviewbyid(r.id.toggle);
    switcher = (switch) findviewbyid(r.id.switcher);
    final linearlayout text = (linearlayout) findviewbyid(r.id.text);
    compoundbutton.oncheckedchangelistener oncheckedchangelistener =
        new compoundbutton.oncheckedchangelistener() {
      @override
      public void oncheckedchanged(
          compoundbutton buttonview, boolean ischecked) {
        if (ischecked) {
          //设置linearlayout垂直布局
          text.setorientation(linearlayout.vertical);
          toggle.setchecked(true);
          switcher.setchecked(true);
        }else {
          //设置水平布局
          text.setorientation(linearlayout.horizontal);
          toggle.setchecked(false);
          switcher.setchecked(false);
        }
      }
    };
    toggle.setoncheckedchangelistener(oncheckedchangelistener);
    switcher.setoncheckedchangelistener(oncheckedchangelistener);
  }
}

其中switch组建的 thumb:@drawable/thumb项参考自:

更多关于android相关内容感兴趣的读者可查看本站专题:《android控件用法总结》、《android开发入门与进阶教程》、《android视图view技巧总结》、《android编程之activity操作技巧总结》、《android数据库操作技巧总结》及《android资源操作技巧汇总

希望本文所述对大家android程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网