Android 开发笔记___switch__开关

时间:2023-03-08 19:04:58

default switch

Android 开发笔记___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__开关