zoukankan      html  css  js  c++  java
  • 安卓学习第8课——开关ToggleButton、Switch

    今天学的是对开关的监听,两种开关ToggleButton和switch

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:orientation="vertical">
    
    <ToggleButton 
        android:id="@+id/togglebutton"
       android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:textOn="纵向排列"
        android:textOff="横向排列"
        android:checked="true" />
    
    <Switch
        android:id="@+id/switcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOn="纵向排列"
        android:textOff="横向排列"
        android:checked="true"/>
    <LinearLayout android:id="@+id/test" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="测试按钮一" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="测试按钮二" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="测试按钮三" /> </LinearLayout> </LinearLayout>

    通过开关控制三个按钮的是水平还是竖直布局,所以需要将三个按钮包裹起来,整体要用线性布局。有一个不懂的是为什么要用android:checked="true"?就是是否要被选中的意思。。。

    package com.example.togglebutton;
    
    import android.app.Activity;
    import android.os.Bundle;
    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 {
        ToggleButton togglebutton;
         Switch switcher;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            togglebutton=(ToggleButton) findViewById(R.id.togglebutton);
            switcher=(Switch) findViewById(R.id.switcher);
            final LinearLayout test=(LinearLayout) findViewById(R.id.test);
            
        OnCheckedChangeListener listener=new OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                // TODO Auto-generated method stub
                if(isChecked){
                    test.setOrientation(1);//竖直
                }else{
                    test.setOrientation(0);//水平
                }
            }
        };
        togglebutton.setOnCheckedChangeListener(listener);
        switcher.setOnCheckedChangeListener(listener);
        }
    }

    这个里面让我体会到一个重点就是数据域,和普通变量。。。

    为什么要将togglebutton和swithcer作为数据域呢?为什么test这个不用作为数据域里的内容呢?

    还有一个就是为什么test这个要用final呢?

  • 相关阅读:
    OK335x mksd.sh hacking
    Qt jsoncpp 对象拷贝、删除、函数调用 demo
    OK335xS 256M 512M nand flash make ubifs hacking
    Qt QScrollArea and layout in code
    JsonCpp Documentation
    Qt 4.8.5 jsoncpp lib
    Oracle数据库生成UUID
    freemarker得到数组的长度
    FreeMarker中if标签内的判断条件
    freemarker语法
  • 原文地址:https://www.cnblogs.com/Yvettey-me/p/3848313.html
Copyright © 2011-2022 走看看