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呢?

  • 相关阅读:
    Eclipse-将svn上的项目转化成相应的项目
    Maven-在eclipse创建maven项目
    Html-Css-div标签嵌套浮动div标签时无法撑开外部div的解决
    Html-Css-a标签的使用
    Jquery-获取父级元素parent
    Jquery-获取子元素children,find
    Jquery-获取同级标签prev,prevAll,next,nextAll
    Jquery-处理iframe的高度自适应
    Html-Css-iframe的自适应高度方案
    Html-Css-iframe的使用
  • 原文地址:https://www.cnblogs.com/Yvettey-me/p/3848313.html
Copyright © 2011-2022 走看看