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. 移动端touch事件实现页面弹动--小插件

    动手之前的打盹 说实话真的是好久没有更新博客了,最近一直赶项目,身心疲惫:最关键的是晚上还要回去上一波王者,实在是忙啊! 这周下来,清闲了些许,或许是因为要到国庆的缘故吧,大家都显得无精打采.俗话说的 ...

  2. getField()和select()方法的区别

    在ThinkPHP中,查询数据库是必不可少的操作. 那么,getField()方法和select()方法都是查询的方法,到底有什么不同呢? 案例来说明: A.select()方法 例子1 $acces ...

  3. 【转】elasticsearch的查询器query与过滤器filter的区别

    很多刚学elasticsearch的人对于查询方面很是苦恼,说实话es的查询语法真心不简单-  当然你如果入门之后,会发现elasticsearch的rest api设计是多么有意思. 说正题,ela ...

  4. httpd网页身份认证

    html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...

  5. 平稳切换nginx版本

    html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...

  6. JavaWeb学习笔记——jquery中的dom操作

     jquery中的dom操作 废话不说:直接上例子: 1.添加节点-html页面 Append:向每个匹配的元素内部追加内容. <body> <ul id="city& ...

  7. SqlServer和Oracle中一些常用的sql语句3 行列转换

    --217, SQL SERVER SELECT Cust_Name , MAX(CASE WHEN Order_Date ='2009-08-01' THEN AR END) "2009- ...

  8. MV45AFZZ 销售订单的增强

    ***INCLUDE MV45AFZZ . *---------------------------------------------------------------------* * FORM ...

  9. android 屏幕适配基础(1)

    1. 概念定义 寸/英寸:   1寸=3.333333厘米:1英寸=2.54厘米(屏幕尺寸以英寸为单位) 像素 :    像素是组成图象的最基本单元要素:点. 一个像素有多大呢?主要取决于显示器的分辨 ...

  10. pm2部署多个nodejs项目配置教程

    实际项目部署中,我们服务器在启动的时候需要自动启动node服务.以前是通过liunx自带的命令启动.但是随着后台微服务越来越多.每次发布新程序.修改脚本太麻烦了.于是换成PM2来做. 1.首先安装pm ...