为了账号安全,请及时绑定邮箱和手机立即绑定

你从未见过的EditText属性详解

前言

Hi,小伙伴们,Layout学会了,ButtonTextView学会了,ImageView也学会了,是不是感觉总是学习这些单一的东西稍微有点枯燥了呢?那么学习了这篇文章之后,开始尽情发挥你们的想象力开始搞事情吧~

这一篇我们讲解EditText的使用,EditText是一个输入框,在Android开发中是常用的控件。也是获取用户数据的一种方式,EditTextTextView的子类,它继承了TextView的所有属性。话不多说,让我们赶紧开始学习吧~

简介

EditText的结构

public class EditText extends TextView

android.view.View
↳ android.widget.TextView
↳ android.widget.EditText

常用属性

1.默认提示文本的两个属性如下

android:hint="默认提示文本" <!--和微信需要登录时一样,在文本框中提示的扣扣号,邮箱-->  
android:textColorHint="#95A1AA" <!--提示的颜色-->

2.获得焦点后全选组件内所有文本内容

android:selectAllOnFocus="true"

3.限制EditText输入类型

	<EditText   
        android:layout_width="match_parent"   
        android:layout_height="wrap_content"   
        android:inputType="phone" />

4.文本类型,多为大写、小写和数字符号,可选参数如下

android:inputType="none" <!--无格式-->  
android:inputType="phone" <!--拨号键盘-->  
android:inputType="text" <!--文本格式-->  
android:inputType="textAutoComplete" <!--自动完成-->  
android:inputType="textAutoCorrect" <!--纠正单词的拼写错误-->  
android:inputType="textCapCharacters" <!--所有字符大写-->  
android:inputType="textCapSentences" <!--仅第一个字母大写-->  
android:inputType="textCapWords" <!--单词首字母大写-->  
android:inputType="textEmailAddress" <!--电子邮件地址格式-->  
android:inputType="textEmailSubject" <!--邮件主题格式-->  
android:inputType="textFilter" <!--文本筛选格式-->  
android:inputType="textImeMultiLine" <!--输入法多行-->  
android:inputType="textLongMessage" <!--长消息格式-->  
android:inputType="textMultiLine" <!--多行输入-->  
android:inputType="textNoSuggestions" <!--不提示-->  
android:inputType="textPassword" <!--密码格式-->  
android:inputType="textPersonName" <!--人名格式-->  
android:inputType="textPhonetic" <!--拼音输入格式-->  
android:inputType="textPostalAddress" <!--邮政格式-->  
android:inputType="textShortMessage" <!--短消息格式-->  
android:inputType="textUri" <!--URI格式-->  
android:inputType="textVisiblePassword" <!--密码可见格式-->  
android:inputType="textWebEditText" <!--作为网页表单的文本格式-->  
android:inputType="textWebEmailAddress" <!--作为网页表单的电子邮件地址格式-->  
android:inputType="textWebPassword" <!--作为网页表单的密码格式-->

数值类型

android:inputType="numberPassword" <!--数字密码格式-->  
android:inputType="numberSigned" <!--有符号数字格式-->  
android:inputType="number" <!--数字格式-->  
android:inputType="numberDecimal" <!--可以带小数点的浮点格式-->  
android:inputType="phone" <!--拨号键盘-->  
android:inputType="datetime" <!--日期-->  
android:inputType="date" <!--日期-->  
android:inputType="time" <!--时间-->

5.设置最小行,最多行,单行

android:minLines="3" <!--设置最小行的行数-->  
android:maxLines="10" <!--最大的行数,当输入内容超过maxlines,文字会自动向上滚动-->  
android:singleLine="true" <!--限制EditText只允许单行输入,而且不会滚动-->

6.设置文字间隔,设置英文字母大写类型

android:textScaleX="1.5" <!--设置字与字的水平间隔-->  
android:textScaleY="1.5" <!--设置字与字的垂直间隔-->  
android:capitalize="none" <!--sentences:仅第一个字母大写;words:每一个单词首字母大小,用空格区分单词;characters:每一个英文字母都大写-->

7.控制EditText四周的间隔距离与内部文字与边框间的距离

android:paddingTop="5dp" <!--使用margin相关属性增加组件相对其他控件的距离,比如android:marginTop="5dp";使用android:paddingTop="5dp"增加组件内文字和组件边框的距离-->

8.改变输入法中回车按钮的显示内容

```xml
android:imeOptions="actionNone"  
<!--imeOptions有下面一些常用值-->  
<!--actionUnspecified未指定,对应常量EditorInfo.IME_ACTION_UNSPECIFIED;-->  
<!--actionNone 没有动作,对应常量EditorInfo.IME_ACTION_NONE;-->  
<!--actionGo执行 “开始” ,对应常量EditorInfo.IME_ACTION_GO;-->  
<!--actionSearch 执行 “搜索”,对应常量EditorInfo.IME_ACTION_SEARCH;-->  
<!--actionSend执行 “发送”,对应常量EditorInfo.IME_ACTION_SEND;-->  
<!--actionNext 执行 “下一个”,对应常量EditorInfo.IME_ACTION_NEXT;-->  
<!--actionPrevious 执行 “上一个”,对应常量IME_ACTION_PREVIOUS;-->  
<!--actionDone 执行 “完成”,对应常量EditorInfo.IME_ACTION_DONE-->
其它常用属性
android:numeric="integer" <!--设置仅仅能输入整数,假设是小数则是:decimal-->  
android:password="true" <!--设置仅仅能输入密码-->  
android:textColor="#ff8c00" <!--字体颜色-->  
android:textStyle="bold" <!--字体 bold, italic, bolditalic-->  
android:textSize="20dp" <!--设置输入文本内容字体大小-->  
android:textAlign="center" <!--EditText没有这个属性,但TextView有,居中-->  
android:typeface="monospace" <!--字型,normal, sans, serif, monospace (标准、无衬线字体、衬线字体、等宽字体)-->  
android:background="@null" <!--背景,这里设置null,意思为透明-->  
android:layout_weight="1" <!--权重,控制控件之间的地位,在控制控件显示的大小时蛮实用的-->  
android:cursorVisible="true" <!--设定光标为显示/隐藏,默认显示-->  
android:digits="1234567890" <!--设置允许输入哪些字符,如“1234567890.+-*/% ()”-->  
android:drawableRight="@drawable/xxx" <!--在EditText的右边输出一个drawable-->  
android:drawableTop="@drawable/xxx" <!--在EditText的正上方输出一个drawable-->  
android:drawableBottom="@drawable/xxx" <!--在EditText的下方输出一个drawable-->  
android:drawableLeft="@drawable/xxx" <!--在EditText的左边输出一个drawable-->  
android:drawablePadding <!--设置text与drawable(图片)的间隔,与drawableLeft、drawableRight、drawableTop、drawableBottom一起使用,可设置为负数,单独使用没有效果-->  
android:editable="true" <!--设置是否可编辑,默认可以编辑-->  
android:ellipsize="start" <!--设置当文字过长时,该控件该怎样显示,例如设置以下值:"start"省略号显示在开头,"end"省略号显示在结尾,"middle"省略号显示在中间,"marquee"以跑马灯的方式显示(动画横向移动)-->  
android:gravity="center" <!--设置文本位置,如设置成"center",文本将居中显示-->
代码实例讲解

通过上面了解EditText输入框的常用属性之后,结合上几讲TextView、Button我们通过代码示例来讲解实现一个简单的登录页面效果

示例xml布局代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF">
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerInParent="true"
        android:layout_marginTop="10dp"
        android:text="登录"
        android:textColor="#008577"
        android:textSize="30sp" />
            
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="180dp"
        android:layout_marginRight="20dp"
        android:orientation="vertical">
        
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="帐号"
                    android:textColor="#000000"
                    android:textSize="18sp" />

                <EditText
                    android:id="@+id/et_mobile"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="20dp"
                    android:layout_weight="1"
                    android:background="#00000000"
                    android:gravity="center"
                    android:hint="请输入手机号"
                    android:inputType="phone"
                    android:maxLength="15"
                    android:singleLine="true"
                    android:textColorHint="#008577"
                    android:textSize="18sp" />

            </LinearLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginTop="10dp"
                android:background="#FF4413" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="密码"
                    android:textColor="#000000"
                    android:textSize="18sp" />

                <EditText
                    android:id="@+id/et_password"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="40dp"
                    android:layout_weight="1"
                    android:background="#00000000"
                    android:gravity="center"
                    android:hint="请输入密码"
                    android:inputType="textPassword"
                    android:maxLength="30"
                    android:singleLine="true"
                    android:textColorHint="#008577"
                    android:textSize="18sp" />
                        
            </LinearLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginTop="10dp"
                android:background="#FF4413" />

        </LinearLayout>

        <Button
            android:id="@+id/btn_login"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:layout_marginTop="55dp"
            android:background="#81D841"
            android:clickable="false"
            android:text="登录"
            android:textColor="#ffffff"
            android:textSize="18sp"
            android:textStyle="bold" />

    </LinearLayout>

</RelativeLayout>
xml布局预览效果图如下

图片描述

常用方法

1.设置焦点,光标的位置

EditText et = (EditText) findViewById(R.id.et_mobile);   
et.setFocusable(true);  
et.requestFocus();  
et.setFocusableInTouchMode(true);  
​  
et.clearFocus();//失去焦点   
et.requestFocus();//获取焦点

2.设置默认输入法

et.setInputType(EditorInfo.TYPE_CLASS_TEXT); //中文键盘  
et.setInputType(EditorInfo.TYPE_TEXT_VARIATION_URI); //英文键盘  
et.setInputType(InputType.TYPE_CLASS_NUMBER); //数字键盘

3.强制显示隐藏软键盘

InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);   
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0); //隐藏软键盘  
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED); //显示软键盘

4.始终不弹出软键盘

//在XML文件中,Edittext父布局上进行如下设置  
android:focusable="true"   
android:focusableInTouchMode="true"  
    
//在Java代码中,添加下面属性  
et.setInputType(InputType.TYPE_NULL);

5.显示隐藏密码

//在XML文件中设置  
android:password="true" <!-- 以”.”形式显示密码文本-->  
android:inputType="textPassword" <!--不可见密码-->  
android:inputType="textVisiblePassword" <!--可见密码-->//在Java代码中设置  
et.setTransformationMethod(HideReturnsTransformationMethod.getInstance());//隐藏密码  
et.setTransformationMethod(PasswordTransformationMethod.getInstance());//显示密码

6.获取文本最大长度,由于EditText没有提供获取最大长度方法,需要用到反射

public static int getMaxLength(EditText et) {
         int length = 0;
         try {
             InputFilter[] inputFilters = et.getFilters();
             for (InputFilter filter : inputFilters) {
                 Class<?> c = filter.getClass();
                 if (c.getName().equals("android.text.InputFilter$LengthFilter")) {
                     Field[] f = c.getDeclaredFields();
                     for (Field field : f) {
                         if (field.getName().equals("mMax")) {
                             field.setAccessible(true);
                             length = (Integer) field.get(filter);
                         }
                     }
                 }
             }
         } catch (Exception e) {
             e.printStackTrace();
         }
         return length;
     }

7.文本监听事件,监控当前输入的文本长度,需要实现TextWatcher接口

private class TextChangedTextWatcher implements TextWatcher {
    
         private EditText mView;
         private int mMaxLength;
         private CharSequence mMobile;
 
         public TextChangedTextWatcher(EditText v) {
             super();
             mView = v;
             mMaxLength = getMaxLength(v);
         }
 
         @Override
         public void beforeTextChanged(CharSequence s, int start, int count, int after) {
             //文本改变前
         }
 
         @Override
         public void onTextChanged(CharSequence s, int start, int before, int count) {
             //文本改变时
             mMobile = s;
         }
 
         @Override
         public void afterTextChanged(Editable s) {
             //文本改变后,一般使用此方法
             if (mMobile == null || mMobile.length() == 0)
                 return;
             if (mMobile.length() == 11 && mMaxLength == 11) {
                 
             }
         }
     }

结语

看完以上内容,是不是也迫不及待想实现一个登录或者注册的页面了!!那还等什么,趁着记忆力深刻,让我们一起开始吧,也请各位多多点赞留言哦~ (WXGZH:下码看花)

点击查看更多内容
1人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
移动开发工程师
手记
粉丝
9
获赞与收藏
13

关注作者,订阅最新文章

阅读免费教程

感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消