自定义 组合控件 与 定义属性

本文介绍了一个自定义的CombineView组件实现过程,该组件结合了TextView和CheckBox的功能,并支持通过XML属性来设置初始文本和选中状态。文章还提供了如何在资源文件中声明自定义属性的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
 * Created by Dash on 2017/11/30.
 *
 * FrameLayout从左上角放置控件
 */
public class CombineView extends FrameLayout implements View.OnClickListener {


    private TextView textView;
    private CheckBox checkBox;
    private String text;
    private boolean checked;


    public CombineView(@NonNull Context context) {
        super(context);
        init();
    }


    public CombineView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);


        //获取xml里面给的属性值
        //第一个参数表示的是命名空间  第二个参数是属性名
        text = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto", "text");
        //第三个参数是默认值
        checked = attrs.getAttributeBooleanValue("http://schemas.android.com/apk/res-auto", "checked", false);


        init();
    }


    public CombineView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    /**
     * 初始化
     */
    private void init() {
        //把布局挂载到CombineView上...viewGroup root表示是否有挂载的父控件

        View view = View.inflate(getContext(), R.layout.combine_layout, this);

// 如果挂载到父控件就不需要添加视图 //this.addView(view);

        //获取里面的控件
        textView = view.findViewById(R.id.text_desc);
        checkBox = view.findViewById(R.id.check_box);


        //设置初始的值
        textView.setText(text);
        checkBox.setChecked(checked);


        //设置点击事件
        this.setOnClickListener(this);


    }


    /**
     * 对外提供设置文本显示的方法
     */
    public void setText(String text){
        textView.setText(text);
    }


    //对外提供设置是否选中
    public void setChecked(boolean flag){
        checkBox.setChecked(flag);
    }


    //获取是否选中
    public boolean getChecked(){
        return checkBox.isChecked();
    }


    @Override
    public void onClick(View view) {
        //点击的时候选中未选中
        checkBox.setChecked(! checkBox.isChecked());
    }

}



在values 中创建values文件------------------------------attrs.xml文件


<resources>
    <!--name="CombineView"以下里面的自定义属性用于哪一个自定义的控件-->
    <declare-styleable name="CombineView">
        <!--name="text"属性名 format="string" 属性的数据类型-->
        <attr name="text" format="string"></attr>
        <attr name="checked" format="boolean"/>
    </declare-styleable>
</resources>


 public Test(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = getContext().obtainStyledAttributes(
                attrs, R.styleable.Test,defStyleAttr, 0);
        float dimensionPixelSize = a.getDimensionPixelSize(
                R.styleable.Test_textg, 0);

//        这个测量的字体更精确  R.styleable.Test_textg  Test_textg组成是:   <declare-styleable name="Test">
        //<attr name="textg" format="dimension" ></attr>
    ///</declare-styleable>
        float dimension = a.getDimension(R.styleable.Test_textg, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 0, getResources().getDisplayMetrics()));

    }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值