default switch

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="10dp" > <TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="left|center_vertical"
android:text="Switch开关:"
android:textColor="#000000"
android:textSize="17sp" /> <LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="1"
android:orientation="vertical" > <Switch
android:id="@+id/sw_status"
android:layout_width="100dp"
android:layout_height="50dp" />
</LinearLayout>
</LinearLayout> <TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:gravity="left"
android:textColor="#000000"
android:textSize="17sp" /> </LinearLayout>
 package com.example.alimjan.hello_world;

 import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView; /**
* Created by alimjan on 7/2/2017.
*/ public class class_3_2_2 extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener { private Switch sw_status;
private TextView tv_result; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.code_3_2_2);
sw_status = (Switch) findViewById(R.id.sw_status);
tv_result = (TextView) findViewById(R.id.tv_result);
sw_status.setOnCheckedChangeListener(this);
refreshResult();
} private void refreshResult() {
String result = String.format("Switch按钮的状态是%s",
(sw_status.isChecked())?"开":"关");
tv_result.setText(result);
} @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
refreshResult();
} public static void startHome(Context mContext) {
Intent intent = new Intent(mContext, class_3_2_2.class);
mContext.startActivity(intent);
}
}

仿IOS风格

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="10dp" > <TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="left|center_vertical"
android:text="仿iOS的开关:"
android:textColor="#000000"
android:textSize="17sp" /> <LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="1"
android:orientation="vertical" > <CheckBox
android:id="@+id/ck_status"
android:layout_width="100dp"
android:layout_height="50dp"
android:background="@drawable/switch_selector"
android:button="@null" />
</LinearLayout>
</LinearLayout> <TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:gravity="left"
android:textColor="#000000"
android:textSize="17sp" /> </LinearLayout>

style

 <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/switch_on"/>
<item android:drawable="@drawable/switch_off"/>
</selector>

java

 package com.example.alimjan.hello_world;

 import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView; /**
* Created by alimjan on 7/2/2017.
*/ public class class_3_2_2_2 extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener { private CheckBox ck_status;
private TextView tv_result; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.code_3_2_2_2);
ck_status = (CheckBox) findViewById(R.id.ck_status);
tv_result = (TextView) findViewById(R.id.tv_result);
ck_status.setOnCheckedChangeListener(this);
refreshResult();
} private void refreshResult() {
String result = String.format("仿iOS开关的状态是%s",
(ck_status.isChecked())?"开":"关");
tv_result.setText(result);
} @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
refreshResult();
} public static void startHome(Context mContext) {
Intent intent = new Intent(mContext, class_3_2_2_2.class);
mContext.startActivity(intent);
}
}

Android 开发笔记___switch__开关的更多相关文章

  1. 【转】Android开发笔记(序)写在前面的目录

    原文:http://blog.csdn.net/aqi00/article/details/50012511 知识点分类 一方面写写自己走过的弯路掉进去的坑,避免以后再犯:另一方面希望通过分享自己的经 ...

  2. Android开发笔记:打包数据库

    对于数据比较多的控制一般会加入SQLite数据库进行数据存储,在打包时这些数据库是不自动打包到apk中的,如何创建数据库呢 方法1:将创建数据库的sql语句在SQLiteHelper继承类中实现,在第 ...

  3. Android开发笔记--hello world 和目录结构

    原文:Android开发笔记--hello world 和目录结构 每接触一个新东西 都有一个hello world的例子. 1.新建项目 2.配置AVD AVD 没有要新建个,如果不能创建 运行SD ...

  4. [APP] Android 开发笔记 003-使用Ant Release 打包与keystore加密说明

    接上节 [APP] Android 开发笔记 002 5. 使用ant release 打包 1)制作 密钥文件 release.keystore (*.keystore) keytool -genk ...

  5. [APP] Android 开发笔记 002-命令行创建默认项目结构说明

    接上节:[APP] Android 开发笔记 001 4. 默认项目结构说明: 这里我使用Sublime Text 进行加载.

  6. Android开发笔记——以Volley图片加载、缓存、请求及展示为例理解Volley架构设计

    Volley是由Google开源的.用于Android平台上的网络通信库.Volley通过优化Android的网络请求流程,形成了以Request-RequestQueue-Response为主线的网 ...

  7. Android开发笔记(一百三十四)协调布局CoordinatorLayout

    协调布局CoordinatorLayout Android自5.0之后对UI做了较大的提升.一个重大的改进是推出了MaterialDesign库,而该库的基础即为协调布局CoordinatorLayo ...

  8. 【转】Android开发笔记——圆角和边框们

    原文地址:http://blog.xianqu.org/2012/04/android-borders-and-radius-corners/ Android开发笔记——圆角和边框们 在做Androi ...

  9. 《ArcGIS Runtime SDK for Android开发笔记》

    开发笔记之基础教程 ArcGIS Runtime SDK for Android 各版本下载地址 <ArcGIS Runtime SDK for Android开发笔记>——(1).And ...

随机推荐

  1. asp.net core权限模块的快速构建

    大部分系统都会有权限模块,别人家系统的权限怎么生成的我不知道,我只知道这样做是可以并且挺好的. 文章中只对asp.net core的部分代码进行说明 呃 记录~,mvc版本自行前往仓库查阅 代码中的一 ...

  2. HDFS概述(2)————Block块大小设置

    以下内容转自:http://blog.csdn.net/samhacker/article/details/23089157?utm_source=tuicool&utm_medium=ref ...

  3. CDH入门

    cloudera(CDH)官网介绍:安装包.离线包该如何下载.官方文档等介绍http://www.aboutyun.com/thread-8908-1-1.html(出处: about云开发) 进入C ...

  4. [C语言]贪吃蛇_结构数组实现

    一.设计思路 蛇身本质上就是个结构数组,数组里存储了坐标x.y的值,再通过一个循环把它打印出来,蛇的移动则是不断地刷新重新打印.所以撞墙.咬到自己只是数组x.y值的简单比较. 二.用上的知识点 结构数 ...

  5. HIVE---基于Hadoop的数据仓库工具讲解

    Hadoop: Hadoop是一个由Apache基金会所开发的分布式系统基础架构.用来开发分布式程序.充分利用集群的威力进行高速运算和存储.Hadoop实现了一个分布式文件系统(Hadoop Dist ...

  6. Centos7虚拟机桥接模式

    主机ping不通虚拟机centos7系统的ip大多有以下原因: 1.主机与centos7ip段对应 (关键是网关.dns服务器要一一对应,具体操作见后文) 2.对应后仍然不通的,可关闭主机与cenos ...

  7. webpack2使用ch7-loader解析css 自动添加浏览器前缀

    1 目录结构  安装的依赖 "autoprefixer": "^6.7.7", "css-loader": "^0.28.0&qu ...

  8. Elasticsearch-sql 用SQL查询Elasticsearch

    Elasticsearch的查询语言(DSL)真是不好写,偏偏查询的功能千奇百怪,filter/query/match/agg/geo各种各样,不管你是通过封装JSON还是通过python/java的 ...

  9. 使用spark对hive表中的多列数据判重

    本文处理的场景如下,hive表中的数据,对其中的多列进行判重deduplicate. 1.先解决依赖,spark相关的所有包,pom.xml spark-hive是我们进行hive表spark处理的关 ...

  10. 从开发到部署会用到的 Docker 命令

    本文的目的是理解容器开发在目标环境中部署的端到端流程,并列出这些操作所需的 Docker 命令.如果有任何Docker问题,请移步到最新最热的技术社区 IT帮 itbang.me 提问,短信通知专家来 ...