您的位置:首页 > 移动开发 > Android开发

Android——EditText自定义边框、圆角和其常用属性总结

2016-12-23 09:51 531 查看
看下效果图:



执行步骤:

首先在/res/layout文件夹下创建custom_et_layout.xml布局文件,源代码如下:

[java] view
plain copy

 print?

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  

    android:layout_width="fill_parent"  

    android:layout_height="wrap_content" >  

    <EditText  

        android:id="@+id/alertDialog_et"  

        android:background="@drawable/bg_edittext"  

        android:paddingLeft="20dp"  

        android:paddingRight="20dp"  

        android:layout_marginTop="30dp"  

        android:layout_marginLeft="25dp"  

        android:layout_marginRight="30dp"  

        android:layout_marginBottom="30dp"  

        android:textColor="#008A00"  

        android:maxLength="3"  

        android:inputType="number"  

        android:layout_width="match_parent"  

        android:layout_height="wrap_content"  

        android:hint="请输入一个有效整数..."/>  

  

</RelativeLayout>  

属性总结:

---------------------------------------------------------------------------------------------

id: 控件的唯一标识,代码中通过id来找到控件

background: 控件的背景,可以通过该属性来自定义控件不同状态下的外观

padding: 控件中文本内容距离控件边框的距离。单位:dp

paddingLeft: 文本到左边框的距离

paddingRight 文本到有边框的距离

layout_marginTop:margin 指控件之间的距离,top指该控件与它上方控件的间距

layout_marginLeft 该控件与它左侧控件的间距

layout_marginRight 该控件与它右侧控件的间距

layout_marginBottom 该控件与它下方控件的间距

textColor 文本颜色

maxLength 文本最大长度,即字符个数

inputType 输入文本的类型,常用的有number:数字;phone:电话号码;email:电子邮件

layout_width 该控件的宽度

layout_height 该控件的高度

hint: 提示文本内容,在点击后自动消失

--------------------------------------------------------------------------------------------

再加上以下内容的渲染,才能得到一个有边框、圆角的EditText。

步骤:

1.在/res/drawable下创建文件bg_edittext_normal.xml,表示该文本框在常规情况下的样子,内容如下:

[java] view
plain copy

 print?

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>  

<shape xmlns:android="http://schemas.android.com/apk/res/android">  

    <solid android:color="@android:color/transparent" />  

    <corners android:radius="10dip"/>  

    <stroke  

        android:width="1dip"  

        android:color="#BDC7D8" />  

</shape></span>  

2.在/res/drawable下创建文件bg_edittext_focused.xml,表示该文本框在获得焦点情况下的样子,内容如下

[java] view
plain copy

 print?

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

<shape xmlns:android="http://schemas.android.com/apk/res/android">  

    <solid android:color="@android:color/transparent" />  

    <corners android:radius="10dip"/>  

    <stroke  

        android:width="1dip"  

        android:color="#728ea3" />  

</shape>  

shape中如果不通过Android:shape来指定形状时,默认是矩形,其中solid代表纯色,corners代表角,radius越大,圆角越大,stroke代表边框线

3.在/res/drawable下创建文件bg_edittext.xml,在选择器中应用以上编写的两个样式,内容如下

[java] view
plain copy

 print?

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

<selector xmlns:android="http://schemas.android.com/apk/res/android">  

    <item  

        android:state_window_focused="false"  

        android:drawable="@drawable/bg_edittext_normal" />  

    <item  

        android:state_focused="true"  

        android:drawable="@drawable/bg_edittext_focused" />  

</selector>  

最后在布局文件的指定控件中的android:background中应用该选择器,例如android:background=“@drawable/bg_edittext”

大功告成!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: