自定义EditText形状 和自定义Checkbox

来源:互联网 发布:浙江2017年专升本数据 编辑:程序博客网 时间:2024/06/05 07:13

一 自定义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"。

默认的情况下


<CheckBox25         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()+"取消选中");                 }             }         }); 









0 0
原创粉丝点击