Android 中 Edittext 样式、焦点、光标等问题整理

    技术2025-04-05  30

    进入页面后自动弹出软键盘需要一下操作

    AndroidManifest.xml中对应的 Activity 中添加

    android:windowSoftInputMode="stateVisible|adjustResize"

    EditText 不用怎么设置

    进入页面后调用此方法获取焦点即可

    private fun showSoftInputFromWindow(editText: EditText) { editText.isFocusable = true editText.isFocusableInTouchMode = true editText.isCursorVisible = true editText.requestFocus() val inputMethodManager = activity?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager inputMethodManager.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT) }

    对应的隐藏软键盘方法

    private fun hideSoftInputFromWindow() { val inputMethodManager = activity?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager inputMethodManager.hideSoftInputFromWindow(activity?.window?.decorView?.windowToken, 0) }

    其他的相关属性,可清楚焦点与光标

    edittext.isFocusable = false edittext.isFocusableInTouchMode = false edittext.clearFocus() edittext.isCursorVisible = false

     

    Edittext 的样式属性设置相关问题

    光标颜色的定义

    首先定义shape

    <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <size android:width="1dp" /> <solid android:color="@color/colorBlack" /> </shape>

    然后设置 android:textCursorDrawable ,我这里是统一到了 style 中

    <item name="android:textCursorDrawable">@drawable/color_cursor</item>

     

    定义键盘的 imeOptions ,这里以简单的 actionDone 为例,其他的属性类似。

    设置属性

    android:imeOptions="actionDone"

    调用setOnEditorActionListener 即可获取监听

    etNickName.setOnEditorActionListener { _, actionId, _ -> if (actionId == EditorInfo.IME_ACTION_DONE) { //todo return@setOnEditorActionListener true } return@setOnEditorActionListener false }

    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

    actionDone 完成,对应常量EditorInfo.IME_ACTION_DONE

    注意:如果设置了 键盘没有变化  那么需要单独加一些其他的属性 配合使用

    xml中 属性设置:

    将singleLine设置为true

    将inputType设置为text 

     

    输入字符 inputType 相关问题

    android:inputType=”none”

    android:inputType=”text”

    android:inputType=”textCapCharacters” 字母大写

    android:inputType=”textCapWords” 首字母大写

    android:inputType=”textCapSentences” 仅第一个字母大写

    android:inputType=”textAutoCorrect” 自动完成

    android:inputType=”textAutoComplete” 自动完成

    android:inputType=”textMultiLine” 多行输入

    android:inputType=”textImeMultiLine” 输入法多行(如果支持)

    android:inputType=”textNoSuggestions” 不提示

    android:inputType=”textUri” 网址

    android:inputType=”textEmailAddress” 电子邮件地址

    android:inputType=”textEmailSubject” 邮件主题

    android:inputType=”textShortMessage” 短讯

    android:inputType=”textLongMessage” 长信息

    android:inputType=”textPersonName” 人名

    android:inputType=”textPostalAddress” 地址

    android:inputType=”textPassword” 密码

    android:inputType=”textVisiblePassword” 可见密码

    android:inputType=”textWebEditText” 作为网页表单的文本

    android:inputType=”textFilter” 文本筛选过滤

    android:inputType=”textPhonetic” 拼音输入

    //数值类型

    android:inputType=”number” 数字

    android:inputType=”numberSigned” 带符号数字格式

    android:inputType=”numberDecimal” 带小数点的浮点格式

    android:inputType=”phone” 拨号键盘

    android:inputType=”datetime” 时间日期

    android:inputType=”date” 日期键盘

    android:inputType=”time” 时间键盘

     

    Processed: 0.010, SQL: 9