• Android 开发笔记___switch__开关


    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 }

  • 相关阅读:
    java前三章总结
    Java入门第二章
    java编程入门小结
    Java入门第二章
    java预习易错点
    计算机基础
    切换卡
    ajax
    水印4
    shuiyin3
  • 原文地址:https://www.cnblogs.com/alimjan/p/7106040.html
Copyright © 2020-2023  润新知