EditText 在开发中也是经常用到的控件,也是一个比较必要的组件,可以说它是用户跟Android应用进行数据传输的窗户。EditText上面有两块内容,一个是提示语,一个是用户输入的字,下面我说一下他的常用属性:
下面以一个登陆模块例举一下EditText的属性:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<EditText
android:id="@+id/et_phone" //设置id
android:layout_width="match_parent" //设置控件宽度
android:layout_height="wrap_content" //设置控件高度
android:layout_marginLeft="20dp" //设置控件与左侧边边距
android:layout_marginRight="20dp" //设置控件与右侧边边距
android:background="@null" //设置EditText背景
android:inputType="number" //设置输入的内容为数字
android:maxLength="11" //设置输入数字个数为11个
android:hint="请输入手机号" //底部阴影提示文字
android:padding="10dp" //设置控件大小
android:drawableBottom="@drawable/shape_et" //设置边框
android:layout_marginTop="20dp"/> //设置控件与上边距距离
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:background="@null"
android:inputType="textPassword"
android:maxLength="16"
android:padding="10dp"
android:drawablePadding="10dp"
android:hint="请输入密码"
android:drawableBottom="@drawable/shape_et_bottom_line"
/>
<TextView
android:id="@+id/tv_login"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="30dp"
android:text="登 录"
android:textColor="#ffffffff"
android:textSize="18sp" />
</LinearLayout>
演示效果:
还有一些属性:
android:layout_gravity="center_vertical" 设置控件显示的位置:默认top,这里居中显示,还有bottom android:hint="请输入数字!"
设置显示在空间上的提示信息 android:numeric="integer" 设置只能输入整数,如果是小数则是:
decimal android:singleLine="true" 设置单行输入,一旦设置为true,则文字不会自动换行。
android:password="true" 设置只能输入密码
android:textColor = "#ff8c00" 字体颜色
android:textStyle="bold" 字体,bold, italic, bolditalic android:textSize="20dip" 大小
android:capitalize = "characters" 以大写字母写
android:textAlign="center" EditText没有这个属性,但TextView有 android:textColorHighlight="#cccccc" 被选中文字的底色,默认为蓝色
android:textColorHint="#ffff00" 设置提示信息文字的颜色,默认为灰色 android:textScaleX="1.5" 控制字与字之间的间距
android:typeface="monospace" 字型,normal, sans, serif, monospace android:background="@null" 空间背景,这里没有,指透明
android:layout_weight="1" 权重,控制控件之间的地位,在控制控件显示的大小时蛮有用的。
android:editable="false" 设置EditText不可编辑
android:singleLine="true" 强制输入的内容在单行
android:ellipsize="end" 自动隐藏尾部溢出数据,一般用于文字内容过长一行无法全部显示时
android:inputType 指定输入法的类型,int类型,可以用|选择多个。取值可以参考:android.text.InputType类。取值包括:text, textUri, phone,number,等。
android:focusable="false"//键盘永远不会弹出
android:typeface="monospace" //设置字型。字形有:normal, sans, serif,monospace
android:maxLength=“50” //设置字数限制
Android:phoneNumber=”true” //输入电话号码
android:password="true" // 以”.”形式显示文本
好了写到这里我头都写大了,不知道差多少,我觉得差不多够用了。EditText是一个神奇的控件希望你能熟练使用它。