仿IOS Switch控件

该篇文章从eoeAndroid搬迁过来的,原文地址:仿IOS Switch控件

前几天写了一个仿ios的segmentcontrol控件( 仿 ios segmentcontrol ),今天突然兴致来了,就写了一个仿ios 的switch控件,由于本人不是学习ps的,所以从ios上截取的图片可能不太完美,希望各位能够见谅。

废话不多少,上代码: 首先是得到自定义的属性,也就一个自定义的属性,就是控件的内容,默认值是on和off,可以在XML文件中引用,填写值的时候需要把打开写在前面,关闭写在后面,并用分号(:)隔开,如(开:关),注意分号是英文的分号。

public SwitchButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        this.context = context;
        TypedArray typedArray = context.obtainStyledAttributes(attrs,
                R.styleable.SwitchButton);
        onOffString = typedArray.getString(R.styleable.SwitchButton_onOff);
        if (onOffString != null && (!"".equals(onOffString))) {
            String[] contentStr = onOffString.split(";");
            if (contentStr.length >= 2) {
                onString = "".equals(contentStr[0]) ? "on" : contentStr[0];
                offString = "".equals(contentStr[1]) ? "off" : contentStr[1];
            } else if (contentStr.length == 1) {
                onString = "".equals(contentStr[0]) ? "on" : contentStr[0];
                offString = "off";
            } else {
                onString = "on";
                offString = "off";
            }
        } else {
            onString = "on";
            offString = "off";
        }
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.view_switchbutton, this);
        switchLayout = (RelativeLayout) findViewById(R.id.switch_layout);
        switchOnBgTextView = (TextView) findViewById(R.id.switch_on_bg_textview);
        switchOffBgTextView = (TextView) findViewById(R.id.switch_off_bg_textview);
        switchOnButtonTextView = (TextView) findViewById(R.id.switch_on_button_textview);
        switchOffButtonTextView = (TextView) findViewById(R.id.switch_off_button_textview);
        switchOnButtonTextView.setTextColor(Color.WHITE);
        switchOnButtonTextView.setText(onString);
        switchOffButtonTextView.setTextColor(Color.GRAY);
        switchOffButtonTextView.setText(offString);
        switchLayout.setOnClickListener(this);
        setView();
    }

然后就是不同的点击事件展示不同的图片

private void setView() {
        if (switchStatues) {
            switchOnBgTextView.setVisibility(View.VISIBLE);
            switchOnButtonTextView.setVisibility(View.VISIBLE);
            switchOffBgTextView.setVisibility(View.GONE);
            switchOffButtonTextView.setVisibility(View.GONE);
        } else {
            switchOnBgTextView.setVisibility(View.GONE);
            switchOnButtonTextView.setVisibility(View.GONE);
            switchOffBgTextView.setVisibility(View.VISIBLE);
            switchOffButtonTextView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.switch_layout:
            switchStatues = !switchStatues;
            setView();
            onCheckedChangeListener.onCheckedChange(switchStatues);
            break;

        default:
            break;
        }
    }

最后附上switch的布局

代码中的点击事件和得到当前的状态

button.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChange(boolean isChecked) {
                // TODO Auto-generated method stub
                if (isChecked) {
                    text.setText("开");
                } else {
                    text.setText("关");
                }
            }
        });
button.getSwitchStatues()

这里写图片描述 这里写图片描述

下载地址:项目代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值