Android自定义组合控件之实现CheckBox变化

前言:自定义组合控件最大的好处就是复用性,可以为我们节省不少代码量,但是一般我们自定义组合控件的时候,封装出来的控件不管是自己用还是别人用也好,封装的代码最好是易读性强,便于修改,没有必要封装太多的属性,一般控制在两三个属性为最佳,毕竟我们不是Google....!好了,扯得有点远了,今天我带领大家来封装一个简单易用的组合控件,让你初步接触如何自定义组合控件!

----------------------------分割线---------------------------

来看效果图:


当然了,这个效果很简单的,你不通过自定义组合控件也可以很轻松的实现。

----------------------------分割线---------------------------

实现:1. 写一个类继承RelativeLayout(ViewGroup)

2. 写布局文件

3. 将布局添加到RelativeLayout中(initView方法)

4. 增加api

5. 自定义属性(1. values/attrs.xml, 2. 声明命名空间 , 3.在自定义view中配置属性, 4. 在自定义view中加载属性值 )

----------------------------分割线---------------------------

自定义属性实现:

一:在values文件下面新建一个attrs的文件然后在里面添加如下代码:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="SettingItemView">
        <attr name="title" format="string" />
        <attr name="desc_on" format="string" />
        <attr name="desc_off" format="string" />
    </declare-styleable>
</resources>
解释:1.declare-styleable中的name是控件的类名。
2.attr里面是控件的属性,可添加字符串,数值,布尔值都可以添加。

二:在构造方法中获取该属性的值:

TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SettingItemView);
String text = ta.getString(R.styleable.SettingItemView_title);

获取这些值之后就可以做进一步的逻辑事件。

三:如何使用自定义控件:

1.在你布局的最外层加入代码:

xmlns:app="http://schemas.android.com/apk/res-auto"
2.使用该属性

  <com.fly.lsn29_groupview.SettingItemView
        android:id="@+id/siv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:desc_off="已经关闭"
        app:desc_on="已经开启"
        app:title="设置" />
----------------------------分割线---------------------------

完整代码:

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class SettingItemView extends RelativeLayout {

    private TextView tvTitle;
    private TextView tvDesc;
    private CheckBox cbCheck;
    private String mDescOn;
    private String mDescOff;

    public SettingItemView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView();
    }

    public SettingItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SettingItemView);
        String text = ta.getString(R.styleable.SettingItemView_title);
        mDescOn = ta.getString(R.styleable.SettingItemView_desc_on);
        mDescOff = ta.getString(R.styleable.SettingItemView_desc_off);
        ta.recycle();
        setTitle(text);
    }

    public SettingItemView(Context context) {
        super(context);
        initView();
    }

    /**
     * 初始化布局
     */
    private void initView() {
        View child = View.inflate(getContext(), R.layout.setting_item_view, null);// 初始化组合控件布局

        tvTitle = (TextView) child.findViewById(R.id.tv_title);
        tvDesc = (TextView) child.findViewById(R.id.tv_desc);
        cbCheck = (CheckBox) child.findViewById(R.id.cb_check);

        this.addView(child);// 将布局添加给当前的RelativeLayout对象
    }

    /**
     * 设置标题
     *
     * @param title
     */
    public void setTitle(String title) {
        tvTitle.setText(title);
    }

    /**
     * 设置表述
     *
     * @param desc
     */
    public void setDesc(String desc) {
        tvDesc.setText(desc);
    }

    /**
     * 判断是否勾选
     *
     * @return
     */
    public boolean isChecked() {
        return cbCheck.isChecked();
    }

    /**
     * 设置选中状态
     *
     * @param checked
     */
    public void setChecked(boolean checked) {
        cbCheck.setChecked(checked);

        // 更新描述信息
        if (checked) {
            setDesc(mDescOn);
        } else {
            setDesc(mDescOff);
        }
    }

}
涉及到的一个简单布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="65dp"
        android:padding="5dp">

        <TextView
            android:id="@+id/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text="标题"
            android:textColor="#000"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/tv_desc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/tv_title"
            android:layout_marginTop="3dp"
            android:text="描述"
            android:textColor="#a000"
            android:textSize="16sp" />

        <CheckBox
            android:id="@+id/cb_check"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:clickable="false"
            android:focusable="false"
            android:focusableInTouchMode="false" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_alignParentBottom="true"
            android:background="#494949" />
    </RelativeLayout>

</LinearLayout>
values文件夹下新建attrs文件添加如下代码:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="SettingItemView">
        <attr name="title" format="string" />
        <attr name="desc_on" format="string" />
        <attr name="desc_off" format="string" />
    </declare-styleable>

</resources>
在布局中使用:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    tools:context="com.fly.lsn29_groupview.MainActivity">

    <com.fly.lsn29_groupview.SettingItemView
        android:id="@+id/siv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:desc_off="已经关闭"
        app:desc_on="已经开启"
        app:title="设置" />

</LinearLayout>
在MainActivity中调用:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final SettingItemView siv = (SettingItemView) findViewById(R.id.siv);
        siv.setChecked(true);//初始化设置
        siv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (siv.isChecked()) {
                    siv.setChecked(false);
                    //处理逻辑
                } else {
                    siv.setChecked(true);
                    //处理逻辑
                }
            }
        });
    }
}
------------------------完!--------------------------









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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

等待着冬天的风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值