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)效果图

Android ToggleButton(开关函数)与switch (开关按钮)的更多相关文章

  1. Android学习笔记-开关按钮ToggleButton和开关Switch

    本节给大家介绍的Android基本UI控件是:开关按钮ToggleButton和开关Switch,这两个其实都是开关组件,只是后者需要在Android 4.0以后才能使用 所以AndroidManif ...

  2. android开关控件Switch和ToggleButton

    序:今天项目中用到了开关按钮控件,查阅了一些资料特地写了这篇博客记录下. 1.Switch <Switch android:id="@+id/bt" android:layo ...

  3. Android——复选按钮和开关按钮

    复选按钮和开关按钮代码如下: <LinearLayout android:layout_width="match_parent" android:layout_height= ...

  4. Android课程---视图组件之开关按钮

    <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="ht ...

  5. Android ToggleButton Example--开关按钮

    Android ToggleButton Example 在 Android 中,  “android.widget.ToggleButton” 是个特殊的类,可以渲染出一个“开关按钮” ,顾名思义, ...

  6. Android内核sysfs中switch类使用实例

    Android内核sysfs中switch类使用实例 最终在这个周末,能够干点自己想要干的事了. 由我这个二流的内核驱动开发人员来解析一下sysfs中的switch类.先猜測一下来历,在普通的嵌入式L ...

  7. Android ToggleButton:状态切换的Button

     Android ToggleButton:状态切换的Button Android ToggleButton和Android Button类似,但是ToggleButton提供了一种选择机制,可以 ...

  8. Android基础控件ToggleButton和Switch开关按钮

    1.简介 ToggleButton和Switch都是开关按钮,只不过Switch要Android4.0之后才能使用! ToggleButton <!--checked 是否选择--> &l ...

  9. 状态开关按钮(ToggleButton)和开关(Switch)

    ToggleButton支持的XML属性及相关方法1.android:checked----->setChecked(boolean) ----->设置该按钮是否被选中2.android: ...

随机推荐

  1. 中国大学MOOC 玩转AutoCAD 熟悉AutoCAD的工作空间

  2. wangEditor富文本编辑器

    设置好了是这样的, 有一个ID问content的编辑框,方便获取,这里的富文本编辑器的版本是2.2 官方文档说3就不支持textarea了 导入一下css 记得css文件夹下应该又3个文件,虽然没有直 ...

  3. 521. Longest Uncommon Subsequence I 最长不同子数组

    [抄题]: [暴力解法]: 时间分析: 空间分析: [优化后]: 时间分析: 空间分析: [奇葩输出条件]: [奇葩corner case]: [思维问题]: [一句话思路]: 两个单词的话,就是看谁 ...

  4. Python学友

    独学而无友,则孤陋而寡闻,python学习过程中希望多和学友交流,一起进步. 开源中国 j_hao104 微信公众号: Pythoner每日一报 https://my.oschina.net/jhao ...

  5. c语言学习笔记 break语句

    比如 for() { for() { break; } } 那个break语句只是跳出它所在的那个for循环,不会把最外面的for循环都跳出去.

  6. jQuery--基础知识速查表

    一.jQuery选择器 选择器 实例 选取 * $("*") 所有元素 #id $("#lastname") id="lastname" 的 ...

  7. readfile()

    readfile()将一个文件写入到输出缓存参数1:文件名

  8. javascript总结3:javaScript的 Math 对象

    Math 对象 Math 对象用于执行数学任务. Math 对象并不像 Date 和 String 那样是对象的类,因此没有构造函数 Math(). Math 常用的方法 var n1=1234; v ...

  9. Java 之集合框架

  10. Android Bundle传递数据

    1.传递普通数据 Intent intent=new Intent(MainActivity.this,TwoActivity.class); Bundle bundle=new Bundle(); ...