Android 开发笔记___switch__开关
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__开关的更多相关文章
- 【转】Android开发笔记(序)写在前面的目录
原文:http://blog.csdn.net/aqi00/article/details/50012511 知识点分类 一方面写写自己走过的弯路掉进去的坑,避免以后再犯:另一方面希望通过分享自己的经 ...
- Android开发笔记:打包数据库
对于数据比较多的控制一般会加入SQLite数据库进行数据存储,在打包时这些数据库是不自动打包到apk中的,如何创建数据库呢 方法1:将创建数据库的sql语句在SQLiteHelper继承类中实现,在第 ...
- Android开发笔记--hello world 和目录结构
原文:Android开发笔记--hello world 和目录结构 每接触一个新东西 都有一个hello world的例子. 1.新建项目 2.配置AVD AVD 没有要新建个,如果不能创建 运行SD ...
- [APP] Android 开发笔记 003-使用Ant Release 打包与keystore加密说明
接上节 [APP] Android 开发笔记 002 5. 使用ant release 打包 1)制作 密钥文件 release.keystore (*.keystore) keytool -genk ...
- [APP] Android 开发笔记 002-命令行创建默认项目结构说明
接上节:[APP] Android 开发笔记 001 4. 默认项目结构说明: 这里我使用Sublime Text 进行加载.
- Android开发笔记——以Volley图片加载、缓存、请求及展示为例理解Volley架构设计
Volley是由Google开源的.用于Android平台上的网络通信库.Volley通过优化Android的网络请求流程,形成了以Request-RequestQueue-Response为主线的网 ...
- Android开发笔记(一百三十四)协调布局CoordinatorLayout
协调布局CoordinatorLayout Android自5.0之后对UI做了较大的提升.一个重大的改进是推出了MaterialDesign库,而该库的基础即为协调布局CoordinatorLayo ...
- 【转】Android开发笔记——圆角和边框们
原文地址:http://blog.xianqu.org/2012/04/android-borders-and-radius-corners/ Android开发笔记——圆角和边框们 在做Androi ...
- 《ArcGIS Runtime SDK for Android开发笔记》
开发笔记之基础教程 ArcGIS Runtime SDK for Android 各版本下载地址 <ArcGIS Runtime SDK for Android开发笔记>——(1).And ...
随机推荐
- 记录maven 整合SSM框架
一.新建maven项目 建好的项目结构如下图: 还需要做以下配置: 勾选上这两项后,就会自动生成 "src/main/java" 和 "src/main/resour ...
- 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 ...
- GCD hdu2588
GCD Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- 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 ...
- bzoj4033(树上染色)
树上染色 有一棵点数为N的树,树边有边权.给你一个在0~N之内的正整数K,你要在这棵树中选择K个点,将其染成黑色,并 将其他的N-K个点染成白色.将所有点染色后,你会获得黑点两两之间的距离加上白点两两 ...
- 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 ...
- 玩转 sublime3 第二弹 ES6环境
安装node: node作为JS的运行环境必须安装 文件下载:https://nodejs.org/dist/v6.11.4/node-v6.11.4-x64.msi 备注:可以去官网 https:/ ...
- swiper拖拽之后不自动滑动问题
//swiper轮播图 var mySwiper = new Swiper('.swiper-container',{ initialSlide :0, autoplay : 3000, direct ...
- OpenVPN CentOS7 安装部署配置详解
一 .概念相关 1.vpn 介绍 vpn 虚拟专用网络,是依靠isp和其他的nsp,在公共网络中建立专用的数据通信网络的技术.在vpn中任意两点之间的链接并没有传统的专网所需的端到端的物理链路,而是利 ...
- 深刻理解反射(Reflection)
最近公司在搞自动化测试,由于版权问题,无法用 '录制脚本' 进行,也就没法用 VS 自带的 UITest 框架(蛋疼), 所以只能开源的 FlaUI 框架来搞了.其中不可避免的涉及到反射的应用,但自己 ...