前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >你从未见过的EditText属性详解

你从未见过的EditText属性详解

作者头像
下码看花
发布2019-09-02 17:02:00
3.2K0
发布2019-09-02 17:02:00
举报
文章被收录于专栏:AndroidStudio初识AndroidStudio初识
前言

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.默认提示文本的两个属性如下

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

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

代码语言:javascript
复制
android:selectAllOnFocus="true"

3.限制 EditText输入类型

代码语言:javascript
复制
<EditText   
        android:layout_width="match_parent"   
        android:layout_height="wrap_content"   
        android:inputType="phone" />

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

代码语言:javascript
复制
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" <!--作为网页表单的密码格式-->

数值类型

代码语言:javascript
复制
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.设置最小行,最多行,单行

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

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

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

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

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

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

代码语言:javascript
复制
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-->
其它常用属性
代码语言:javascript
复制
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布局代码

代码语言:javascript
复制
<?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.设置焦点,光标的位置

代码语言:javascript
复制
EditText et = (EditText) findViewById(R.id.et_mobile); 
et.setFocusable(true);
et.requestFocus();
et.setFocusableInTouchMode(true);

et.clearFocus();//失去焦点
et.requestFocus();//获取焦点

2.设置默认输入法

代码语言:javascript
复制
et.setInputType(EditorInfo.TYPE_CLASS_TEXT); //中文键盘
et.setInputType(EditorInfo.TYPE_TEXT_VARIATION_URI); //英文键盘
et.setInputType(InputType.TYPE_CLASS_NUMBER); //数字键盘

3.强制显示隐藏软键盘

代码语言:javascript
复制
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);  
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0); //隐藏软键盘
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED); //显示软键盘

4.始终不弹出软键盘

代码语言:javascript
复制
//在XML文件中,Edittext父布局上进行如下设置
android:focusable="true"   
android:focusableInTouchMode="true"

//在Java代码中,添加下面属性
et.setInputType(InputType.TYPE_NULL);

5.显示隐藏密码

代码语言:javascript
复制
//在XML文件中设置
android:password="true" <!-- 以”.”形式显示密码文本-->
android:inputType="textPassword" <!--不可见密码-->
android:inputType="textVisiblePassword" <!--可见密码-->

//在Java代码中设置
et.setTransformationMethod(HideReturnsTransformationMethod.getInstance());//隐藏密码
et.setTransformationMethod(PasswordTransformationMethod.getInstance());//显示密码

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

代码语言:javascript
复制
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接口

代码语言:javascript
复制
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) {

             }
         }
     }
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-08-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 下码看花 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 简介
    • 常用属性
      • 其它常用属性
        • 代码实例讲解
          • xml布局预览效果图如下
            • 常用方法
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档