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. 判断手机使用网络wifi 2G 3G

    ConnectivityManager cManager = (ConnectivityManager) this .getSystemService(Context.CONNECTIVITY_SER ...

  2. map两种插入方法解析(insert() 与 下标[]操作)

    insert 含义是: 如果key存在,则插入失败,如果key不存在,就创建这个key-value. 实例: map.insert((key, value)) 利用下标操作的含义是: 如果这个key存 ...

  3. 【bzoj1024】[SCOI2009]生日快乐

    1024: [SCOI2009]生日快乐 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 2372  Solved: 1717[Submit][Statu ...

  4. springMVC+spring+mybatis整合(包括文件上传和下载)

    driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncod ...

  5. 更改windows服务的配置文件(app.config)必须重启服务才能生效吗?

    这个问题是前一阶段写windows服务碰到的.本来在写获取配置文件的某个配置的值的时候,通常我都是写类似下面的这么一个静态方法来获取: 1: /// <summary> 2: /// 获取 ...

  6. ssh时传递环境变量

    设置要传递的变量: -o SendEnv=Varname 但是不是每个都能传,受服务器上sshd_config里的下面两个选项的控制: AcceptEnv and PermitUserEnvironm ...

  7. numpy ndarray 返回 index 问题

    经常遇到需要返回满足条件的index. python中没有which函数,但有列表推导式, 可以实现类似功能 y= np.array([3,2,5,20]) yOut[131]: array([ 3, ...

  8. 正则表达式复习 (?<=) (?=)

    1.首先值得一说的是"<" 和">" 不是元字符 "."是元字符 ,连接字符"-",即使在字符组内部也不一定 ...

  9. js实现在表格中删除和添加一行

    <!DOCTYPE html><html> <head> <title> new document </title> <meta ht ...

  10. Http中的身份传递

    IIS默认的身份验证方式 身份传递策略包括使用操作系统的委派功能或在应用程序级传递票证和/或凭证 为了阻止IIS的身份验证委派,可以在web.config加入如下设置, <system.web& ...