原文:How to create custom Switch in Android
示例源码下载:http://download.csdn.net/detail/zhangyihui1986/8651437
在本教程中,我会演示如何定制Switch控件,添加单击监听器,使用Switch去控制媒体声音和wifi设备。
下面是本教程的运行结果:
本教程在Eclipse 4.2.0上开发。
首先,修改app的主布局文件
打开“res/layout/main.xml”文件,添加两个Switch控件,以及几个TextView和Button。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Disable all switch"
android:layout_marginBottom="15dp"
android:onClick="button_click"/>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/switch1"
android:layout_alignParentLeft="true"
android:layout_marginBottom="10dp"
android:layout_marginLeft="20dp"
android:text="Media Volume"/>
<Switch
android:id="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="OFF"
android:textOn="ON"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:onClick="onSwitchClicked"
android:thumb="@drawable/switch_bg"
android:track="@drawable/track_bg"
android:layout_marginBottom="15dp" />
<TextView android:layout_below="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/switch2"
android:layout_alignParentLeft="true"
android:layout_marginBottom="10dp"
android:layout_marginLeft="20dp"
android:text="Wifi"/>
<Switch android:layout_below="@+id/switch1"
android:id="@+id/switch2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="OFF"
android:textOn="ON"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:onClick="onSwitchClicked"
android:thumb="@drawable/switch_bg"
android:track="@drawable/track_bg"
android:layout_marginBottom="15dp" />
</RelativeLayout>
<TextView android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="27px"
android:layout_marginTop="15dp"
android:text="Show spinner choice"
android:gravity="center"
android:textColor="#CD2134"
android:textStyle="bold" />
</LinearLayout>
需要注意Switch的两个属性:
android:thumb="@drawable/switch_bg" android:track="@drawable/track_bg"
我们需要在drawable目录中创建两个XML文件“switch_bg.xml”和”track_bg.xml“用来定制Switch控件。
res/drawable/switch_bg.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="@drawable/enable" />
<item android:state_pressed="true" android:drawable="@drawable/press" />
<item android:state_checked="true" android:drawable="@drawable/check_on" />
<item android:drawable="@drawable/enable" />
</selector>
res/drawable/track_bg.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="@drawable/track_disable" />
<item android:drawable="@drawable/track_default" />
</selector>
代码
在Switch控件XML定义中使用android:onClick属性给所有Switch控件添加单击监听器:android:onClick=”onSwitchClicked “
在布局对应的Activity中,下面的方法处理所有Switch控件的单击事件,一个Switch控制媒体声音的开关,另一个控制Wifi 的开关。
public void onSwitchClicked(View view) {
switch(view.getId()){
case R.id.switch1:
if(switch1.isChecked()) {
textview.setText("Switch 1 check ON by click on it");
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
8, 0);
}
else {
textview.setText("Switch 1 check OFF by click on it");
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
0, 0);
}
break;
case R.id.switch2:
if(switch2.isChecked()) {
textview.setText("Switch 2 check ON by click on it");
wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);
}
else {
textview.setText("Switch 2 check OFF by click on it");
wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(false);
}
break;
}
}
怎样控制Switch的setOnCheckedChangeListener
这个方法会在用户拖动Switch上的”拇指图(thumb)“或者点击Switch时触发,这时做与Switch控件的onClick方法同样的操作
switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if (buttonView.isChecked()){
textview.setText("Switch 1 check ON by drag thumb");
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 8, 0); }
else{
textview.setText("Switch 1 check OFF by drag thumb");
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
}
}
});
switch2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if (buttonView.isChecked()){
textview.setText("Switch 2 check ON by drag thumb");
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);
}
else{
textview.setText("Switch 2 check OFF by drag thumb");
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(false);
}
}
});
最后,添加按钮onClick方法来禁用或启用Switch控件
public void button_click(View view){
if(is_enable == true) {
is_enable = false;
button.setText("Enable all switch");
textview.setText("Switch is Disable by click on button");
} else {
is_enable = true;
button.setText("Disable all switch");
textview.setText("Switch is Enable by click on button");
}
switch1.setEnabled(is_enable);
switch2.setEnabled(is_enable);
}
注意:要想控制Wifi设备,需要在AndroidManifest文件中添加下面的权限
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
Demo
运行应用
点击第一个Switch控件打开/关闭媒体声音设备
拖动第二个Switch控件的”拇指图“开关Wifi
点击按钮启用或禁用所有的Switch控件
你可以从这里下载本教程所有的源码
参考: http://developer.android.com/reference/android/widget/Switch.html