自定义EditText形状 和自定义Checkbox

本文详细介绍了如何在Android应用中自定义EditText和CheckBox的样式,包括使用XML文件来定制EditText的形状,并通过状态选择器来实现CheckBox的自定义样式。同时,文章还展示了如何在Activity中为CheckBox设置监听器,以便根据用户操作更新UI。

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


一 自定义EditText


<EditText android:id="@+id/etSearch"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
               
                android:background="@drawable/button_search"
               
                />


button_search.xml 来定制EditText的形状

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!-- 填充透明颜色 -->
    <solid android:color="@color/white" />

    <!-- 定义边框线条 -->
    <stroke
        android:width="1dp"
        android:color="@color/gray" />

    <!-- 圆角角度 -->
    <corners
        android:radius="5dp"/>

    <padding android:bottom="5dp"
        android:top="5dp"
        android:left="5dp"
        android:right="5dp"/>
	</shape>
效果图

二 自定义CheckBox 

CheckBox和Button一样,也是一种古老的控件,它的优点在于,不用用户去填写具体的信息,只需轻轻点击,缺点在于只有“是”和“否”两种情况,但我们往往利用它的这个特性,来获取用户的一些信息。

checkBox是由两部分组成的,一部分是前面的选择框,还有一个文本内容。要只使用选择框时,需要另 android:button="@bull"。

默认的情况下


<CheckBox
25         android:id="@+id/checkbox2"
26         android:layout_width="match_parent"
27         android:layout_height="wrap_content"
28         android:text="应用程序是在平等的条件下创建的"    >      
29     </CheckBox>

自定义

<CheckBox
               android:id="@+id/choose_all_cb"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
             <span style="white-space:pre">	</span>android:textSize="@dimen/content_size"
                android:paddingLeft="13dp"
                android:paddingRight="13dp"
                android:layout_gravity="center"

                android:button="@null" // 使原生态的checkbox消失
                android:drawableLeft="@drawable/checkbox_checked_bg" 使用新的checkbox图片
                android:checked="false" />
checkbox_checked_bg.xml 状态选择器

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

最后在Activity中设置监听

 checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){ 
            @Override 
            public void onCheckedChanged(CompoundButton buttonView, 
                    boolean isChecked) { 
                // TODO Auto-generated method stub 
                if(isChecked){ 
                   // editText1.setText(buttonView.getText()+"选中"); 
                }else{ 
                    //editText1.setText(buttonView.getText()+"取消选中"); 
                } 
            } 
        }); 









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值