一、前言:

EditText与TextView非常相似,它甚至与TextView 共用了绝大部分XML属性和方法。EditText与TextView的最大区别在于:EditText可以接受用户输入

EditText的使用_android

键盘改为搜索按钮.png

二、功能介绍:

1、EditText 支持的 XML 属性及相关方法

XML 属性

相关方法

说明

android:text

setText(CharSequence text)

设置文本内容

android:textColor

setTextColor(int color)

字体颜色

android:hint

setHint(int resid)

内容为空时候显示的文本

android:textColorHint

void setHintTextColor(int color)

为空时显示的文本的颜色

android:inputType

setInputType(int type)

限制输入类型(none:禁止输入number:整数类型numberDecimal:小数点类型date:日期类型text:文本类型(默认值)phone:拨号键盘textPassword:密码textVisiblePassword:可见密码textUri:网址

android:maxLength

setMaxLines(int maxlines)

限制显示的文本长度,超出部分不显示

android:minLines

setMinLines(int maxlines)

设置文本的最小行数

android:gravity

setGravity(int gravity)

设置文本位置,如设置成“center”,文本将居中显示。

android:drawableLeft

Drawable left,Drawable top,Drawable right, Drawable bottom

在text的左边输出一个drawable,如图片

android:drawablePadding

设置text与drawable(图片)的间隔,与drawableLeftdrawableRightdrawableTopdrawableBottom一起使用,可设置为负数,单独使用没有效果。

android:digits

设置允许输入哪些字符。如“1234567890”

android:ellipsize

设置当文字过长时,该控件该如何显示(start:省略号显示在开头end:省略号显示在结尾middle:省略号显示在中间marquee:以跑马灯的方式显示(动画横向移动))。

android:lines

setLines(int lines)

设置文本的行数,设置两行就显示两行,即使第二行没有数据。

android:lineSpacingExtra

设置行间距

android:singleLine

setSingleLine()

true:单行显示 false:可以多行

android:textStyle

设置字形,可以设置一个或多个,用"

"隔开(bold:粗体italic:斜体bolditalic:又粗又斜

android:typeface

normal:正常的、sans:无、serif:截线、monospace:等宽字体

android:background

设置EditText背景."@null"设置背景为透明.当我们设置背景后,EditText的那条线就会消失

android:imeOptions

normal:完成actionGo:下一步actionSearch:搜素actionNext、下一步actionDone:完成actionPrevious:操作上一个

设置右下角IME动作与编辑框相关的动作,如actionDone右下角将显示一个“完成”,而不设置默认是一个回车符号

android:lineSpacingExtra

设置行间距.

android:lineSpacingMultiplier

设置行间距的倍数. 如设置成1.5倍.

2、android:inputType="none"不起作用

android:inputType="none"是用于指定输入类型的属性,它的作用是禁用软键盘的弹出。
但是,它只在EditText控件中起作用,如果你在其他控件中使用它,它可能不会起作用。如果你在EditText控件中使用了android:inputType=“none”,但是软键盘仍然弹出,可能是因为你的EditText控件没有禁止获取焦点。
如果你的EditText控件设置了android:focusable="false"或android:focusableInTouchMode=“false”,那么它将不会获取焦点,android:inputType="none"也不会起作用。你需要将这些属性设置为true,才能让EditText控件获取焦点并禁用软键盘的弹出。

//一起用
android:focusable="false"
android:inputType="none"

3、 android:imeOptions="actionSearch"不起作用

imeOptions和inputType需要一起使用才能生效

//一起用
android:imeOptions="actionSearch"
android:inputType="text"

4、监听输入事件

/**
         * 监听输入过程的变化
         */
        edit.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(
                charSequence: CharSequence,
                start: Int,
                before: Int,
                after: Int
            ) {
                Log.d("lyy","----------内容改变之前调用:-------------${charSequence}")
            }

            override fun onTextChanged(
                charSequence: CharSequence,
                start: Int,
                before: Int,
                after: Int
            ) {
                Log.d("lyy","----------内容改变中:-------------${charSequence}")
            }

            override fun afterTextChanged(editable: Editable) {
                //输入完成,处理业务逻辑.....
                Log.d("lyy","----------内容改变之后调用:-------------${editable}")
            }
        })
/***
         *
         * 监听搜索按钮或者完成按钮的点击事件
         * 1、XML变更为搜索图标:android:imeOptions="actionSearch"
         * 2、XML变更为完成图标: android:imeOptions="actionDone"
         * 3、XML变更为发送图标: android:imeOptions="actionSend"
         */
       edit.setOnEditorActionListener(object : TextView.OnEditorActionListener{
            override fun onEditorAction(v: TextView?, actionId: Int, event: KeyEvent?): Boolean {
                when(actionId){
                    EditorInfo.IME_ACTION_SEARCH ->{
                        //处理搜索按钮的业务逻辑.....
                        Log.d("lyy","----------点击搜索按钮--------------}")
                        return true
                    }

                    EditorInfo.IME_ACTION_DONE ->{
                        //处理完成按钮的业务逻辑.....
                        Log.d("lyy","----------点击完成按钮--------------}")
                        return true
                    }
                    EditorInfo.IME_ACTION_SEND ->{
                        //处理完成按钮的业务逻辑.....
                        Log.d("lyy","----------点击发送按钮--------------}")
                        return true
                    }

                }

                return false
            }
        })

5、XML使用

<EditText
        android:id="@+id/edit"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:hint="请输入搜索内容"
        app:layout_constraintTop_toBottomOf="@+id/tv_title"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginTop="20dp"
        android:layout_marginHorizontal="30dp"
        android:background="@drawable/btn_shap_30"
        android:imeOptions="actionSearch"
        android:inputType="text"
        android:lines="1"
        android:paddingHorizontal="22dp"
        android:typeface="monospace"
        />

btn_shap_30.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">

    <solid android:color="#FFF0F5"/>

    <corners
        android:radius="25dp" />
</shape>