default switch

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:orientation="vertical"
  5. android:padding="10dp" >
  6.  
  7. <LinearLayout
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:orientation="horizontal"
  11. android:paddingTop="10dp" >
  12.  
  13. <TextView
  14. android:layout_width="0dp"
  15. android:layout_height="match_parent"
  16. android:layout_weight="1"
  17. android:gravity="left|center_vertical"
  18. android:text="Switch开关:"
  19. android:textColor="#000000"
  20. android:textSize="17sp" />
  21.  
  22. <LinearLayout
  23. android:layout_width="0dp"
  24. android:layout_height="wrap_content"
  25. android:layout_gravity="right"
  26. android:layout_weight="1"
  27. android:orientation="vertical" >
  28.  
  29. <Switch
  30. android:id="@+id/sw_status"
  31. android:layout_width="100dp"
  32. android:layout_height="50dp" />
  33. </LinearLayout>
  34. </LinearLayout>
  35.  
  36. <TextView
  37. android:id="@+id/tv_result"
  38. android:layout_width="match_parent"
  39. android:layout_height="wrap_content"
  40. android:paddingTop="10dp"
  41. android:gravity="left"
  42. android:textColor="#000000"
  43. android:textSize="17sp" />
  44.  
  45. </LinearLayout>
  1. package com.example.alimjan.hello_world;
  2.  
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.widget.CompoundButton;
  8. import android.widget.Switch;
  9. import android.widget.TextView;
  10.  
  11. /**
  12. * Created by alimjan on 7/2/2017.
  13. */
  14.  
  15. public class class_3_2_2 extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
  16.  
  17. private Switch sw_status;
  18. private TextView tv_result;
  19.  
  20. @Override
  21. protected void onCreate(Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.code_3_2_2);
  24. sw_status = (Switch) findViewById(R.id.sw_status);
  25. tv_result = (TextView) findViewById(R.id.tv_result);
  26. sw_status.setOnCheckedChangeListener(this);
  27. refreshResult();
  28. }
  29.  
  30. private void refreshResult() {
  31. String result = String.format("Switch按钮的状态是%s",
  32. (sw_status.isChecked())?"开":"关");
  33. tv_result.setText(result);
  34. }
  35.  
  36. @Override
  37. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  38. refreshResult();
  39. }
  40.  
  41. public static void startHome(Context mContext) {
  42. Intent intent = new Intent(mContext, class_3_2_2.class);
  43. mContext.startActivity(intent);
  44. }
  45. }

仿IOS风格

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:orientation="vertical"
  5. android:padding="10dp" >
  6.  
  7. <LinearLayout
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:orientation="horizontal"
  11. android:paddingTop="10dp" >
  12.  
  13. <TextView
  14. android:layout_width="0dp"
  15. android:layout_height="match_parent"
  16. android:layout_weight="1"
  17. android:gravity="left|center_vertical"
  18. android:text="仿iOS的开关:"
  19. android:textColor="#000000"
  20. android:textSize="17sp" />
  21.  
  22. <LinearLayout
  23. android:layout_width="0dp"
  24. android:layout_height="wrap_content"
  25. android:layout_gravity="right"
  26. android:layout_weight="1"
  27. android:orientation="vertical" >
  28.  
  29. <CheckBox
  30. android:id="@+id/ck_status"
  31. android:layout_width="100dp"
  32. android:layout_height="50dp"
  33. android:background="@drawable/switch_selector"
  34. android:button="@null" />
  35. </LinearLayout>
  36. </LinearLayout>
  37.  
  38. <TextView
  39. android:id="@+id/tv_result"
  40. android:layout_width="match_parent"
  41. android:layout_height="wrap_content"
  42. android:paddingTop="10dp"
  43. android:gravity="left"
  44. android:textColor="#000000"
  45. android:textSize="17sp" />
  46.  
  47. </LinearLayout>

style

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

java

  1. package com.example.alimjan.hello_world;
  2.  
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.widget.CheckBox;
  8. import android.widget.CompoundButton;
  9. import android.widget.TextView;
  10.  
  11. /**
  12. * Created by alimjan on 7/2/2017.
  13. */
  14.  
  15. public class class_3_2_2_2 extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
  16.  
  17. private CheckBox ck_status;
  18. private TextView tv_result;
  19.  
  20. @Override
  21. protected void onCreate(Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.code_3_2_2_2);
  24. ck_status = (CheckBox) findViewById(R.id.ck_status);
  25. tv_result = (TextView) findViewById(R.id.tv_result);
  26. ck_status.setOnCheckedChangeListener(this);
  27. refreshResult();
  28. }
  29.  
  30. private void refreshResult() {
  31. String result = String.format("仿iOS开关的状态是%s",
  32. (ck_status.isChecked())?"开":"关");
  33. tv_result.setText(result);
  34. }
  35.  
  36. @Override
  37. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  38. refreshResult();
  39. }
  40.  
  41. public static void startHome(Context mContext) {
  42. Intent intent = new Intent(mContext, class_3_2_2_2.class);
  43. mContext.startActivity(intent);
  44. }
  45. }

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. 记录maven 整合SSM框架

    一.新建maven项目 建好的项目结构如下图: 还需要做以下配置: 勾选上这两项后,就会自动生成 "src/main/java"   和 "src/main/resour ...

  2. D - DZY Loves Hash CodeForces - 447A

    DZY has a hash table with p buckets, numbered from 0 to p - 1. He wants to insert n numbers, in the ...

  3. GCD hdu2588

    GCD Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  4. hdu3507 Print Article(斜率DP优化)

    Zero has an old printer that doesn't work well sometimes. As it is antique, he still like to use it ...

  5. bzoj4033(树上染色)

    树上染色 有一棵点数为N的树,树边有边权.给你一个在0~N之内的正整数K,你要在这棵树中选择K个点,将其染成黑色,并 将其他的N-K个点染成白色.将所有点染色后,你会获得黑点两两之间的距离加上白点两两 ...

  6. AIM Tech Round 4 (Div. 2)ABCD

    A. Diversity time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  7. 玩转 sublime3 第二弹 ES6环境

    安装node: node作为JS的运行环境必须安装 文件下载:https://nodejs.org/dist/v6.11.4/node-v6.11.4-x64.msi 备注:可以去官网 https:/ ...

  8. swiper拖拽之后不自动滑动问题

    //swiper轮播图 var mySwiper = new Swiper('.swiper-container',{ initialSlide :0, autoplay : 3000, direct ...

  9. OpenVPN CentOS7 安装部署配置详解

    一 .概念相关 1.vpn 介绍 vpn 虚拟专用网络,是依靠isp和其他的nsp,在公共网络中建立专用的数据通信网络的技术.在vpn中任意两点之间的链接并没有传统的专网所需的端到端的物理链路,而是利 ...

  10. 深刻理解反射(Reflection)

    最近公司在搞自动化测试,由于版权问题,无法用 '录制脚本' 进行,也就没法用 VS 自带的 UITest 框架(蛋疼), 所以只能开源的 FlaUI 框架来搞了.其中不可避免的涉及到反射的应用,但自己 ...