1. 程式人生 > >Android - SwitchButton開關按鈕

Android - SwitchButton開關按鈕

工具類 :

package utils;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;

public
class SwitchButtonView extends RelativeLayout { public static final int OPEN = 1; public static final int CLOSE = 0; private int defaultStatus; private int currentStatus; private OnSwitchListener onSwitchListener; private TypedArray typedArray; private RelativeLayout relativeLayout;
private View view; public SwitchButtonView(Context context) { super(context); } public SwitchButtonView(Context context, AttributeSet attrs) { super(context, attrs); LayoutInflater.from(context).inflate(R.layout.layout_switch, this); typedArray = context.
obtainStyledAttributes(attrs, R.styleable.SwitchButton); initView(); init(); } /** * 初始化控制元件 */ private void initView() { relativeLayout = findViewById(R.id.relative_layout_bg); view = findViewById(R.id.view_scroll); } /** * 初始化操作 */ private void init() { defaultStatus = typedArray.getInt(R.styleable.SwitchButton_defaultStatus, 1); if (defaultStatus == 0) { relativeLayout.setBackgroundResource(R.drawable.bg_switch_bottom); view.setBackgroundResource(R.drawable.bg_switch_top); setViewLocation(view, RelativeLayout.ALIGN_PARENT_LEFT); currentStatus = CLOSE; } else if (defaultStatus == 1) { relativeLayout.setBackgroundResource(R.drawable.bg_switch_bottom_open); view.setBackgroundResource(R.drawable.bg_switch_top_open); setViewLocation(view, RelativeLayout.ALIGN_PARENT_RIGHT); currentStatus = OPEN; } } private void setViewLocation(View view, int location) { LayoutParams layoutParams = (LayoutParams) view.getLayoutParams(); if (location == RelativeLayout.ALIGN_PARENT_LEFT) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT); } } else if (location == RelativeLayout.ALIGN_PARENT_RIGHT) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_LEFT); } } layoutParams.addRule(location); view.setLayoutParams(layoutParams); } /** * 獲取當前的狀態 */ public int getCurrentStatus() { return currentStatus; } /** * 設定狀態變化監聽 */ public void setOnSwitchListener(OnSwitchListener onSwitchListener) { this.onSwitchListener = onSwitchListener; } /** * 關閉按鈕 */ public void closeButton() { relativeLayout.setBackgroundResource(R.drawable.bg_switch_bottom); view.setBackgroundResource(R.drawable.bg_switch_top); setViewLocation(view, RelativeLayout.ALIGN_PARENT_LEFT); currentStatus = CLOSE; } /** * 開啟按鈕 */ public void openButton() { relativeLayout.setBackgroundResource(R.drawable.bg_switch_bottom_open); view.setBackgroundResource(R.drawable.bg_switch_top_open); setViewLocation(view, RelativeLayout.ALIGN_PARENT_RIGHT); currentStatus = OPEN; } /** * 改變狀態 */ private void changeStatus() { if (currentStatus == OPEN) { closeButton(); } else if (currentStatus == CLOSE) { openButton(); } if (onSwitchListener != null) { onSwitchListener.onSwitchChange(); //呼叫監聽改變時候處理邏輯的函式 } } /** * 觸控事件 * 觸控一下,改變按鈕的狀態 */ @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: /** * 改變狀態 */ changeStatus(); break; default: break; } return super.onTouchEvent(event); } public interface OnSwitchListener { void onSwitchChange(); } }

佈局裡 :

<utils.SwitchButtonView
    android:id="@+id/switch_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    app:defaultStatus="open"/>

Activity中 :

// 開啟 : 
switchButton.openButton();
// 關閉 : 
switchButton.closeButton();
// 加監聽 : 
switchButton.setOnSwitchListener(new SwitchButtonView.OnSwitchListener() {
    @Override
    public void onSwitchChange() {
        LogUtil.e(TAG, "switchButton.getCurrentStatus() --- " + switchButton.getCurrentStatus());
    }
});
// 1為開啟, 0為關閉