自定义控件,checkbox

本文详细介绍了如何自定义控件和组合控件的过程,包括布局文件的使用、自定义类的继承与构造、属性的自定义与使用等关键步骤。同时,提供了自定义控件的命名空间、属性定义、属性使用方法、构造方法实现等实例,帮助开发者深入了解自定义控件的创建与应用。

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

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">checkBox组件 天生有焦点 所以无法通过onclickListener监听 </span>

所以可以设置

 android:focusable="false"
        android:clickable="false"

来取消checkbox的功能

 通过监听其他组件 来改变checkbox的状态


自定义控件


1.把自定义组件 写在一个布局文件中

<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<<strong>RelativeLayout</strong> xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="60dip" >

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dip"
        android:layout_marginTop="8dip"
        android:text="设置是否自动更新"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/tv_desc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_title"
        android:layout_marginLeft="10dip"
        android:text="自动更新已经关闭"
        android:textColor="@android:color/darker_gray"
        android:textSize="18dp" />
<!-- 因为checkBox天生有焦点 无法通过改变焦点的形式来更改内容 所以设置为 没有焦点 不可点击 通过监听整个相对布局
来改变checkbox的checked状态
-->
    <CheckBox
        android:focusable="false"
        android:clickable="false"
        android:id="@+id/cb_status"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="10dp"
        android:gravity="center_vertical" />

    <View
        android:layout_width="wrap_content"
        android:layout_height="0.2dip"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="8dip"
        android:layout_marginRight="8dip"
        android:background="#000000" />


</RelativeLayout></span>
<strong style="font-size:14px; font-family: Arial, Helvetica, sans-serif;">2.写一个自定义类 继承相应的布局类</strong>
<span style="font-family:Arial, Helvetica, sans-serif;font-size:14px;"><strong>把先前写好的布局文件 转化成一个View</strong></span>
public class SettingItemView extends <strong>RelativeLayout</strong> {
	private CheckBox cb_status;
	private TextView tv_title;
	private TextView tv_desc;

	/**
	 * 初始化布局文件
	 * @param context
	 */
	private void initView(Context context) {
		// <strong>把一个布局文件转化成View 并加载在this对象中</strong>
		<strong>View.inflate(context, R.layout.setting_item_view, this);</strong>
		// 因为已经把setting_item_view加载到SettingItemView中 所以可以用this.find....
		cb_status = (CheckBox) this.findViewById(R.id.cb_status);
		tv_title = (TextView) this.findViewById(R.id.tv_title);
		tv_desc = (TextView) this.findViewById(R.id.tv_desc);

	}

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

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

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

	
}

3.在界面布局文件中引用

<span style="font-size:14px;"><<strong>com.DLJ.mobilesafe.ui.SettingItemView ///写全包名</strong>
        android:id="@+id/siv_update"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" /></span>




自定义组合控件的过程


1.自定义一个View 一般来说,继承相对布局,或者线性布局  ViewGroup;
2.实现父类的构造方法。一般来说,需要在构造方法里初始化自定义的布局文件;
3.根据一些需要或者需求,定义一些API方法;


----------------------------------
4.根据需要,自定义控件的属性,可以参照TextView属性;


5.自定义命名空间,例如:
  xmlns:itheima="http://schemas.android.com/apk/res/《包名》"
  xmlns:itheima="http://schemas.android.com/apk/res/com.itheima.mobilesafe"


6.自定义我们的属性,在Res/values/attrs.xml
//系统会根据我们的命名空间去找读取attrs.xml文件
可以参照sdk的attrs.xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>


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


</resources>


7.使用我们自定义的属性
 例如:
        itheima:title="设置自动更新"
        itheima:desc_on="设置自动更新已经开启"
        itheima:desc_off="设置自动更新已经关闭"


8.在我们自定义控件的带有两个参数的构造方法里AttributeSet attrs 取出我们的属性值,关联自定义布局文件对应的控件;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值