ToggleButton 状态开关按钮及Switch开关

本文介绍了如何在Android应用中使用自定义ToggleButton,通过监听OnCheckedChangeListener事件来切换文本显示,并结合select选择器实现开关外观变化。此外,还展示了如何创建一个MyButtonView类,实现滑动式的开关控制,响应用户的触摸操作。
摘要由CSDN通过智能技术生成

第一步

<ToggleButton
    android:id="@+id/toggle_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/togglebutton_select"  //自定义开关背景
    android:textOn="打开"            //打开时,显示文本打开
    android:textOff="关闭"/>         //默认关闭,显示文本关闭

第二步在代码中运行

    toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){
                Toast.makeText(ToggleButtonActivity.this,"打开",Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(ToggleButtonActivity.this,"关闭",Toast.LENGTH_SHORT).show();
            }
        }
    });
}

第三步select选择器

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/icon3"/>
    <item android:state_checked="false" android:drawable="@drawable/icon4"/>
</selector>

自定义switch开关

public class MyButtonView extends View  {
    public Paint paint;
    public int slidingMax, slidingLength;
    Bitmap bitmap1;
    Bitmap bitmap2;
    public float startx, lastx;
    boolean ischeck = true;
    boolean Enable = true;


    public MyButtonView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initview();
    }

    private void initview() {
        paint = new Paint();
        paint.setAntiAlias(true);
        bitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.switch_background);
        bitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.slide_button);
        slidingMax = bitmap1.getWidth() - bitmap2.getWidth();
        this.setOnClickListener(l);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(bitmap1.getWidth(), bitmap1.getHeight());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(bitmap1, 0, 0, paint);
        canvas.drawBitmap(bitmap2, slidingLength, 0, paint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastx = startx = event.getX();
                Enable = true;
                break;
            case MotionEvent.ACTION_MOVE:
                float endx = event.getX();
                float distance = endx - startx;
                slidingLength += distance;
                if (slidingLength < 0) {
                    slidingLength = 0;
                } else if (slidingLength > slidingMax) {
                    slidingLength = slidingMax;
                }
                invalidate();
                startx = event.getX();
                if (Math.abs(endx - lastx) > 5) {
                    Enable = false;
                }
                break;
            case MotionEvent.ACTION_UP:
                if (Enable == false) {
                    if (slidingLength < slidingMax / 2) {
                        ischeck = false;
                    } else {
                        ischeck = true;
                    }
                    flush();
                }
                break;
        }
        return true;
    }

    private void flush() {
        if (ischeck == true) {
            ischeck = false;
            slidingLength = slidingMax;
        } else {
            ischeck = true;
            slidingLength = 0;
        }
        invalidate();
    }

View.OnClickListener l=new OnClickListener() {
    @Override
    public void onClick(View v) {
        if(Enable==true){
            flush();
        }
    }
};
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值