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 ...
随机推荐
- Tree(uva 536)
先声明,我还在学习中,这个题大部分代码借鉴的大佬的,其实这算是比较经典二叉树题了,关键在于递归建树. 代码附上: #include <iostream> #include <cstr ...
- Linux脚本入门(1)
1. Linux 脚本编写基础1.1 语法基本介绍1.1.1 开头程序必须以下面的行开始(必须方在文件的第一行): #!/bin/sh 符号#!用来告诉系统它后面的参数是用来执行该文件的程序.在这个例 ...
- JSP入门 生命周期
我们之前使用的都是javax.servlet.http.HttpServlet,这个类实现了javax.servlet.Servlet接口,而这个接口中定义的三个方法是所有servlet都必须实现的. ...
- VirtualBox安装linux mint教程
准备工作: 1.VirtualBox安装包,官方下载页面. 2.linux mint镜像iso文件,官方下载页面. 安装过程: 1.打开VirtualBox后点击新建,在弹出界面选择专家模式,类型选择 ...
- 简单又炫酷的two.js 二维动画教程
前 言 S N 今天呢给大家介绍一个小js框架,Two.JS.其实在自己学习的过程中并没有找到合适的教程,所以我这种学习延迟的同学是有一定难度的,然后准备给大家整理一份,简单易懂的小教程 ...
- 【原创】流程引擎的网关(遵循BPMN2.0)设计总结
概述 BPMN 2.0是什么呢?业务流程模型注解(Business Process Modeling Notation - BPMN)是 业务流程模型的一种标准图形注解.这个标准 是由对象管理组(Ob ...
- 在Ubuntu上安装Docker
原文链接:https://docs.docker.com/engine/installation/linux/ubuntu/ 这里记录的是社区版安装方式,由于平时只做开发使用所以不需要安装企业版, ...
- apache一个ip多个端口虚拟主机
1.打开httpd.conf,查找Listen:80,在下面一行加入Listen:8080:2.查找#Include conf/extra/httpd-vhosts.conf,将此行前面的#去掉:3. ...
- 基于搜狗搜索的微信公众号爬虫实现(C#版本)
Author: Hoyho Luo Email: luohaihao@gmail.com Source Url:http://here2say.me/11/ 转载请保留此出处 本文介绍基于搜狗的微信公 ...
- ASP.NET没有魔法——ASP.NET MVC 与数据库大集合
ASP.NET没有魔法——ASP.NET与数据库 ASP.NET没有魔法——ASP.NET MVC 与数据库之MySQL ASP.NET没有魔法——ASP.NET MVC 与数据库之ORM ASP.N ...