<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/white1"/>
<corners android:radius="8dip"/>
</shape>
<a.b.c.SlideButton
android:layout_width="300px"
android:layout_height="48px"
android:content="@+id/slideContentLayout"
android:handle="@+id/slideHandle"
android:background="@drawable/slide_round"
android:orientation="horizontal"
android:id="@+id/slideButton">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight = "1"
android:background="#00000000"
android:id="@+id/slideContentLayout">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="@color/black4"
android:gravity="center"
android:textSize="14sp"
android:text="Off"
android:textStyle="bold"/>
</LinearLayout>
<Button
android:layout_width="70px"
android:layout_height="fill_parent"
android:id="@+id/slideHandle"
android:background="@color/main_color"
android:text="ON"
android:textStyle="bold"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight = "1"
android:background="#00000000"
android:id="@+id/slideContentLayout">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="@color/black4"
android:gravity="center"
android:textSize="14sp"
android:text="Off"
android:textStyle="bold"/>
</LinearLayout>
</a.b.c.SlideButton>
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Checkable;
import android.widget.SlidingDrawer;
import android.widget.SlidingDrawer.OnDrawerCloseListener;
import android.widget.SlidingDrawer.OnDrawerOpenListener;
public class SlideButton extends SlidingDrawer implements Checkable, OnDrawerOpenListener, OnDrawerCloseListener{
int mButtonResource = 0;
OnCheckChangedListner mOnCheckChangedListner;
public SlideButton(Context context, AttributeSet attr){
super(context, attr);
setOnDrawerOpenListener(this);
setOnDrawerCloseListener(this);
}
@Override
public boolean isChecked() {
// TODO Auto-generated method stub
return !isOpened();
}
@Override
public void setChecked(boolean checked) {
// TODO Auto-generated method stub
if(!isOpened() != checked){
if(checked){
this.animateClose();
}else{
this.animateOpen();
}
}
}
@Override
public void toggle() {
// TODO Auto-generated method stub
if(!isOpened()){
this.animateOpen();
}else{
this.animateClose();
}
}
public interface OnCheckChangedListner{
public void onCheckChanged(View v, boolean isChecked);
}
@Override
public void onDrawerOpened() {
// TODO Auto-generated method stub
if(mOnCheckChangedListner != null){
mOnCheckChangedListner.onCheckChanged(this, isChecked());
}
}
@Override
public void onDrawerClosed() {
// TODO Auto-generated method stub
if(mOnCheckChangedListner != null){
mOnCheckChangedListner.onCheckChanged(this, isChecked());
}
}
public OnCheckChangedListner getOnCheckChangedListner() {
return mOnCheckChangedListner;
}
public void setOnCheckChangedListner(
OnCheckChangedListner onCheckChangedListner) {
this.mOnCheckChangedListner = onCheckChangedListner;
}
}
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.slide_button);
sb = (SlideButton)findViewById(R.id.slideButton);
sb.setOnCheckChangedListner(new SlideButton.OnCheckChangedListner() {
@Override
public void onCheckChanged(View v, boolean isChecked) {
// TODO Auto-generated method stub
Log.i("aaa","bbb");
Toast t = Toast.makeText(context, Boolean.toString(isChecked), Toast.LENGTH_LONG);
t.show();
}
});