• ToggleButton控件,Switch控件


    (一)

    1.效果图

      

     2. activity_main.xml

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     xmlns:tools="http://schemas.android.com/tools"
     4     android:layout_width="match_parent"
     5     android:layout_height="match_parent"
     6     android:paddingBottom="@dimen/activity_vertical_margin"
     7     android:paddingLeft="@dimen/activity_horizontal_margin"
     8     android:paddingRight="@dimen/activity_horizontal_margin"
     9     android:paddingTop="@dimen/activity_vertical_margin"
    10     tools:context="com.example.app4.MainActivity"
    11     android:orientation="vertical">
    12 
    13     <ToggleButton
    14         android:id="@+id/tb_button"
    15         android:textOn="纵向布局"
    16         android:textOff="横向布局"
    17         android:checked="true"
    18         android:layout_width="wrap_content"
    19         android:layout_height="wrap_content" />
    20 
    21     <LinearLayout
    22         android:id="@+id/ll"
    23         android:orientation="vertical"
    24         android:layout_width="wrap_content"
    25         android:layout_height="wrap_content">
    26         <Button
    27             android:text="按钮1"
    28             android:layout_width="wrap_content"
    29             android:layout_height="wrap_content" />
    30         <Button
    31             android:text="按钮2"
    32             android:layout_width="wrap_content"
    33             android:layout_height="wrap_content" />
    34         <Button
    35             android:text="按钮3"
    36             android:layout_width="wrap_content"
    37             android:layout_height="wrap_content" />
    38     </LinearLayout>
    39 </LinearLayout>

    3.MainActivity.java

     1 package com.example.app4;
     2 
     3 import android.support.v7.app.AppCompatActivity;
     4 import android.os.Bundle;
     5 import android.widget.CompoundButton;
     6 import android.widget.LinearLayout;
     7 import android.widget.ToggleButton;
     8 
     9 public class MainActivity extends AppCompatActivity {
    10     private ToggleButton toggleButton;
    11     private LinearLayout linearLayout;
    12 
    13 
    14     @Override
    15     protected void onCreate(Bundle savedInstanceState) {
    16         super.onCreate(savedInstanceState);
    17         setContentView(R.layout.activity_main);
    18         toggleButton = (ToggleButton) findViewById(R.id.tb_button);
    19         linearLayout = (LinearLayout) findViewById(R.id.ll);
    20 
    21         toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    22             @Override
    23             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    24                 if (isChecked){
    25                     //设置纵向布局
    26                     linearLayout.setOrientation(LinearLayout.VERTICAL);
    27                     toggleButton.setChecked(true);
    28                 }else {
    29                     //设置纵向布局
    30                     linearLayout.setOrientation(LinearLayout.HORIZONTAL);
    31                     toggleButton.setChecked(false);
    32                 }
    33             }
    34         });
    35     }
    36 }

     (二)

    1.效果图

        

    2.activity_main.xml

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     xmlns:tools="http://schemas.android.com/tools"
     4     android:layout_width="match_parent"
     5     android:layout_height="match_parent"
     6     android:paddingBottom="@dimen/activity_vertical_margin"
     7     android:paddingLeft="@dimen/activity_horizontal_margin"
     8     android:paddingRight="@dimen/activity_horizontal_margin"
     9     android:paddingTop="@dimen/activity_vertical_margin"
    10     tools:context="com.example.app4.MainActivity"
    11     android:orientation="vertical">
    12 
    13     <ToggleButton
    14         android:id="@+id/tb_button"
    15         android:textOn="纵向布局"
    16         android:textOff="横向布局"
    17         android:checked="true"
    18         android:layout_width="wrap_content"
    19         android:layout_height="wrap_content" />
    20     <Switch
    21         android:id="@+id/sw"
    22         android:checked="true"
    23         android:layout_width="wrap_content"
    24         android:layout_height="wrap_content" />
    25 
    26     <LinearLayout
    27         android:id="@+id/ll"
    28         android:orientation="vertical"
    29         android:layout_width="wrap_content"
    30         android:layout_height="wrap_content">
    31         <Button
    32             android:text="按钮1"
    33             android:layout_width="wrap_content"
    34             android:layout_height="wrap_content" />
    35         <Button
    36             android:text="按钮2"
    37             android:layout_width="wrap_content"
    38             android:layout_height="wrap_content" />
    39         <Button
    40             android:text="按钮3"
    41             android:layout_width="wrap_content"
    42             android:layout_height="wrap_content" />
    43     </LinearLayout>
    44 </LinearLayout>

    3.MainActivity.java

     1 package com.example.app4;
     2 
     3 import android.support.v7.app.AppCompatActivity;
     4 import android.os.Bundle;
     5 import android.widget.CompoundButton;
     6 import android.widget.LinearLayout;
     7 import android.widget.Switch;
     8 import android.widget.ToggleButton;
     9 
    10 public class MainActivity extends AppCompatActivity {
    11     private ToggleButton toggleButton;
    12     private LinearLayout linearLayout;
    13     private Switch aSwitch;
    14 
    15 
    16     @Override
    17     protected void onCreate(Bundle savedInstanceState) {
    18         super.onCreate(savedInstanceState);
    19         setContentView(R.layout.activity_main);
    20         toggleButton = (ToggleButton) findViewById(R.id.tb_button);
    21         linearLayout = (LinearLayout) findViewById(R.id.ll);
    22         aSwitch = (Switch) findViewById(R.id.sw);
    23 
    24         MyListener myListener = new MyListener();
    25 
    26 
    27         toggleButton.setOnCheckedChangeListener(myListener);
    28         aSwitch.setOnCheckedChangeListener(myListener);
    29 
    30 
    31         /*toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    32             @Override
    33             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    34                 if (isChecked){
    35                     //设置纵向布局
    36                     linearLayout.setOrientation(LinearLayout.VERTICAL);
    37                     toggleButton.setChecked(true);
    38                 }else {
    39                     //设置纵向布局
    40                     linearLayout.setOrientation(LinearLayout.HORIZONTAL);
    41                     toggleButton.setChecked(false);
    42                 }
    43             }
    44         });*/
    45     }
    46 
    47     class  MyListener implements CompoundButton.OnCheckedChangeListener{
    48         @Override
    49         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    50             if (isChecked){
    51                 //设置纵向布局
    52                 linearLayout.setOrientation(LinearLayout.VERTICAL);
    53                 toggleButton.setChecked(true);
    54                 aSwitch.setChecked(true);
    55             }else {
    56                 //设置纵向布局
    57                 linearLayout.setOrientation(LinearLayout.HORIZONTAL);
    58                 toggleButton.setChecked(false);
    59                 aSwitch.setChecked(false);
    60             }
    61         }
    62     }
    63 }
  • 相关阅读:
    uboot中setenv和saveenv分析
    DMA和通道的区别
    openwrt 切换overlay文件系统为根文件系统
    华为SDN:解决传统网络3大问题
    企业需要申请多大宽带的专线?如何节省专线费用?
    stp
    inotify文件监控
    Qt中C++与QML交互
    内核空间可以直接访问应用层空间地址
    linux 提权漏洞总结
  • 原文地址:https://www.cnblogs.com/sunxiaoyan/p/9039279.html
Copyright © 2020-2023  润新知