zoukankan      html  css  js  c++  java
  • 017 Android ToggleButton(开关函数)与switch (开关按钮)

    1.ToggleButton 

    (1)介绍

    (2)组件形状

    (3)xml文件设置

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">
    
    
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
    
            <!--设置背景颜色,ToggleButton 默认灰色背景看不出来-->
            <ToggleButton
                android:id="@+id/toggleButton2"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="ToggleButton"
                android:background="#000fff"
                android:textOff="关灯"
                android:textOn="开灯" />
    
            <ImageView
                android:id="@+id/imageView"
                android:layout_width="200dp"
                android:layout_height="300dp"
                app:srcCompat="@mipmap/img1" />
    
    
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="100dp"
            android:layout_marginTop="20dp"
            android:background="#345555"
            android:orientation="horizontal">
    
            <Switch
                android:id="@+id/switch1"
                android:layout_width="200dp"
                android:layout_height="100dp"
                android:layout_weight="1"
                android:text="客厅灯开关" />
        </LinearLayout>
    </LinearLayout>

    (4)后台代码

    对应的工程名:test22

    package com.lucky.test22;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.CompoundButton;
    import android.widget.ImageView;
    import android.widget.Switch;
    import android.widget.ToggleButton;
    
    public class MainActivity extends AppCompatActivity {
    
        ToggleButton toggleButton;
        ImageView imageView;
        Switch aSwitch;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            toggleButton=findViewById(R.id.toggleButton2);
            aSwitch=findViewById(R.id.switch1);
            imageView=findViewById(R.id.imageView);
            toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if(isChecked){
                        imageView.setImageResource(R.mipmap.img1);  //修改所显示的图片
                    }else {
                        imageView.setImageResource(R.mipmap.img2);
                    }
                }
            });
    
            aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if(isChecked){
                        imageView.setImageResource(R.mipmap.img1);  //修改所显示的图片
                    }else {
                        imageView.setImageResource(R.mipmap.img2);
                    }
                }
            });
        }
    }

     (5)效果图

  • 相关阅读:
    Scrum 冲刺博客第五篇
    Scrum 冲刺博客第四篇
    Scrum 冲刺博客第三篇
    ajax send()
    form action中get post传递参数的问题
    struts2 iterator中if标签的使用
    表格内容自动换行
    从js向Action传中文参数出现乱码问题的解决方法
    java开发环境搭建
    Can not find the tag library descriptor for "http://java.sun.com/jsp/jstl/
  • 原文地址:https://www.cnblogs.com/luckyplj/p/10494711.html
Copyright © 2011-2022 走看看