You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现Android中EditText及自定义键盘强制固定预设输入内容?

Android实现两个特殊输入需求

一、让EditText始终显示预设内容

通过监听输入变化,强制将EditText内容替换为预设文本,同时避免触发无限循环回调。

实现方式1:使用TextWatcher

private const val FIXED_CONTENT = "固定显示的内容"

val fixedEditText = findViewById<EditText>(R.id.et_fixed)
fixedEditText.addTextChangedListener(object : TextWatcher {
    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}

    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}

    override fun afterTextChanged(s: Editable?) {
        if (s.toString() != FIXED_CONTENT) {
            // 临时移除监听防止循环触发
            fixedEditText.removeTextChangedListener(this)
            s?.replace(0, s.length, FIXED_CONTENT)
            fixedEditText.addTextChangedListener(this)
        }
    }
})

实现方式2:使用InputFilter(更简洁)

fixedEditText.filters = arrayOf(InputFilter { _, _, _, _, _, _ ->
    FIXED_CONTENT
})

二、自定义键盘,所有操作都输入预设短语

自定义键盘布局并拦截所有按键事件,无论用户点击哪个按键,都插入指定短语。

1. 创建自定义键盘布局(res/xml/custom_keyboard.xml)

<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:keyWidth="33%p"
    android:keyHeight="60dp"
    android:horizontalGap="2dp"
    android:verticalGap="2dp">

    <Row>
        <Key android:codes="1" android:keyLabel="键1" />
        <Key android:codes="2" android:keyLabel="键2" />
        <Key android:codes="3" android:keyLabel="键3" />
    </Row>
    <Row>
        <Key android:codes="4" android:keyLabel="键4" />
        <Key android:codes="5" android:keyLabel="键5" />
        <Key android:codes="-5" android:keyLabel="删除" />
    </Row>
</Keyboard>

2. 代码关联键盘与EditText

private const val PRESET_PHRASE = "自动输入的预设短语"

val targetEditText = findViewById<EditText>(R.id.et_target)
val customKeyboard = Keyboard(this, R.xml.custom_keyboard)
val keyboardView = findViewById<KeyboardView>(R.id.keyboard_view)

keyboardView.keyboard = customKeyboard
keyboardView.setOnKeyboardActionListener(object : KeyboardView.OnKeyboardActionListener {
    override fun onPress(primaryCode: Int) {}
    override fun onRelease(primaryCode: Int) {}

    override fun onKey(primaryCode: Int, keyCodes: IntArray?) {
        // 无论点击哪个键,都插入预设短语
        targetEditText.text.insert(targetEditText.selectionStart, PRESET_PHRASE)
        // 若需要保留删除功能,可单独判断:
        // if (primaryCode == -5) targetEditText.text.delete(targetEditText.selectionStart -1, targetEditText.selectionStart)
    }

    override fun onText(text: CharSequence?) {}
    override fun swipeLeft() {}
    override fun swipeRight() {}
    override fun swipeDown() {}
    override fun swipeUp() {}
})

// 屏蔽系统键盘,点击EditText显示自定义键盘
targetEditText.showSoftInputOnFocus = false
targetEditText.setOnTouchListener { _, _ ->
    keyboardView.visibility = View.VISIBLE
    false
}

内容的提问来源于stack exchange,提问作者Gerbzy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 17:05:06