ToggleButton(开关按钮)是Android系统中比较简单的一个组件,是一个具有选中和未选择状态双状态的按钮,并且需要为不同的状态设置不同的显示文本。下面以一个简单的控制室内设备的开关为例来介绍开关按钮在安卓开发中的使用。
首先,创建一个名为ToggleButtonLib的一个库,用于设置ToggleButton的相关参数,其中部分代码描述如下:
public class ToggleButton extends View{
private SpringSystem springSystem;
private Spring spring ;
/** */
private float radius;
/** 开启颜色*/
private int onColor = Color.parseColor("#4ebb7f");
/** 关闭颜色*/
private int offBorderColor = Color.parseColor("#dadbda");
/** 灰色带颜色*/
private int offColor = Color.parseColor("#ffffff");
/** 手柄颜色*/
private int spotColor = Color.parseColor("#ffffff");
/** 边框颜色*/
private int borderColor = offBorderColor;
/** 画笔*/
private Paint paint ;
/** 开关状态*/
private boolean toggleOn = false;
/** 边框大小*/
private int borderWidth = 2;
/** 垂直中心*/
private float centerY;
/** 按钮的开始和结束位置*/
private float startX, endX;
/** 手柄X位置的最小和最大值*/
private float spotMinX, spotMaxX;
/**手柄大小 */
private int spotSize ;
/** 手柄X位置*/
private float spotX;
/** 关闭时内部灰色带高度*/
private float offLineWidth;
/** */
private RectF rect = new RectF();
/** 默认使用动画*/
private boolean defaultAnimate = true;
private OnToggleChanged listener;
private ToggleButton(Context context) {
super(context);
}
public ToggleButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setup(attrs);
}
public ToggleButton(Context context, AttributeSet attrs) {
super(context, attrs);
setup(attrs);
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
spring.removeListener(springListener);
}
public void onAttachedToWindow() {
super.onAttachedToWindow();
spring.addListener(springListener);
}
public void setup(AttributeSet attrs) {
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStyle(Style.FILL);
paint.setStrokeCap(Cap.ROUND);
springSystem = SpringSystem.create();
spring = springSystem.createSpring();
spring.setSpringConfig(SpringConfig.fromOrigamiTensionAndFriction(50, 7));
this.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
toggle(defaultAnimate);
}
});
TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.ToggleButton);
offBorderColor = typedArray.getColor(R.styleable.ToggleButton_offBorderColor, offBorderColor);
onColor = typedArray.getColor(R.styleable.ToggleButton_onColor, onColor);
spotColor = typedArray.getColor(R.styleable.ToggleButton_spotColor, spotColor);
offColor = typedArray.getColor(R.styleable.ToggleButton_offColor, offColor);
borderWidth = typedArray.getDimensionPixelSize(R.styleable.ToggleButton_borderWidth, borderWidth);
defaultAnimate = typedArray.getBoolean(R.styleable.ToggleButton_animate, defaultAnimate);
typedArray.recycle();
borderColor = offBorderColor;
}
public void toggle() {
toggle(true);
}
public void toggle(boolean animate) {
toggleOn = !toggleOn;
takeEffect(animate);
if(listener != null){
listener.onToggle(toggleOn);
}
}
新建一个xml文件用来展示室内控制界面,在当前项目中导入先前创建好的库文件后,布局文件中的部分代码如下:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_margin="10dp"
android:layout_gravity="center_vertical"
android:background="@drawable/corner_bg"
>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/air1" />
<TextView
android:id="@+id/textView1"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:text="西向空调 状态:关闭"
android:textSize="15sp" />
<com.zcw.togglebutton.ToggleButton
android:id="@+id/tb1"
android:layout_width="54dp"
android:layout_height="30dp"
android:layout_marginTop="5dp"
android:layout_marginLeft="150dp"/>
LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_margin="10dp"
android:layout_gravity="center_vertical"
android:background="@drawable/corner_bg"
>
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/air2" />
<TextView
android:id="@+id/textView2"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:text="东向空调 状态:关闭"
android:textSize="15sp" />
<com.zcw.togglebutton.ToggleButton
android:id="@+id/tb2"
android:layout_width="54dp"
android:layout_height="30dp"
android:layout_marginTop="5dp"
android:layout_marginLeft="150dp"/>
LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_margin="10dp"
android:layout_gravity="center_vertical"
android:background="@drawable/corner_bg"
>
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/fan1" />
<TextView
android:id="@+id/textView3"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:text="一号电扇(奥斯牌) 状态:关闭"
android:textSize="15sp" />
<com.zcw.togglebutton.ToggleButton
android:id="@+id/tb3"
android:layout_width="54dp"
android:layout_height="30dp"
android:layout_marginTop="5dp"
android:layout_marginLeft="80dp"/>
LinearLayout>
在其对应的Java文件中通过对开关按钮的控制来达到控制室内设备的开关显示:
public class Home_Air_Control_Activity extends Activity {
private com.zcw.togglebutton.ToggleButton tb1;
private com.zcw.togglebutton.ToggleButton tb2;
private com.zcw.togglebutton.ToggleButton tb3;
private TextView tv_state1;
private TextView tv_state2;
private TextView tv_state3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_air);
SysApplication.getInstance().addActivity(this);
tb1=(ToggleButton)findViewById(R.id.tb1);
tb2=(ToggleButton)findViewById(R.id.tb2);
tb3=(ToggleButton)findViewById(R.id.tb3);
tv_state1=(TextView)findViewById(R.id.textView1);
tv_state2=(TextView)findViewById(R.id.textView2);
tv_state3=(TextView)findViewById(R.id.textView3);
tb1.setOnToggleChanged(new OnToggleChanged(){
public void onToggle(boolean on){
if(on){
tv_state1.setText("西向空调 状态:开启中");
}
else{
tv_state1.setText("西向空调 状态:关闭");
}
}
});
tb2.setOnToggleChanged(new OnToggleChanged(){
public void onToggle(boolean on){
if(on){
tv_state2.setText("东向空调 状态:开启中");
}
else{
tv_state2.setText("东向空调 状态:关闭");
}
}
});
tb3.setOnToggleChanged(new OnToggleChanged(){
public void onToggle(boolean on){
if(on){
tv_state3.setText("一号电扇(奥斯牌) 状态:开启中");
}
else{
tv_state3.setText("一号电扇(奥斯牌) 状态:关闭");
}
}
});
}
}