Android入门----Switch控件


    Switch是Android的一个开关控件,但是该控件是4.0以后才有得,故而有些项目需要的时候不得不自己去实现该控件功能,网上主要流行的方法是继承View等控件自己在onDraw()里面绘制控件,但是不是效果不太理想就是体验性太差,另外也有修改官方Switch控件的,个人觉得修改官方Switch控件比较靠谱,比较体验性方面性能方面都有保证

一 、使用Switch控件自带的开关效果


效果图:




 activity_main.xml中的代码:

<span style="font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Switch
        android:id="@+id/switch1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Switch控件" 
        />

</LinearLayout></span>

MainActivity.java中的代码:


<span style="font-size:18px;">package com.test.switchdemo;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Switch;
import android.widget.Toast;
import android.os.Build;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		Switch switch1 = (Switch) findViewById(R.id.switch1);
		switch1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (buttonView.isChecked()) {
					Toast.makeText(MainActivity.this,
							"开始" + buttonView.getText().toString(),
							Toast.LENGTH_LONG).show();
				} else {
					Toast.makeText(MainActivity.this,
							"关闭" + buttonView.getText().toString(),
							Toast.LENGTH_LONG).show();
				}
			}
		});
	}

}</span>


二、自定义Switch控件的开关效果


效果图:



 activity_main.xml中的代码:


<span style="font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Switch
        android:id="@+id/switch1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Switch控件" 
        android:thumb="@drawable/thumb_selctor"
        android:track="@drawable/track_selctor"
        />

</LinearLayout></span>

增加了两个属性thumb和track,解释如下:

android:track:底部的图片
android:thumb:滑块的图片



drawable文件夹下面的thumb_selctor.xml中的代码:

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- 按下鼠标,显示白色的原型图片 -->
    <item android:drawable="@drawable/yuan_white" android:state_pressed="true"/>
    <!-- 松开鼠标,显示棕色的原型图片 -->
    <item android:drawable="@drawable/yuan_orange" android:state_pressed="false"/>

</selector></span>


drawable文件夹下面的track_selctor.xml中的代码:

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- 蓝色的背景条,打开时显示该背景 -->
    <item android:drawable="@drawable/back_on" android:state_checked="true"/>
    <!-- 橘黄色的背景条,关闭时选择该背景 -->
    <item android:drawable="@drawable/back_off" android:state_checked="false"/>

</selector></span>



MainActivity.java中的代码:同上


代码Demo下载点这里




















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值