【问题标题】:Input connection for numeric type keyboard数字键盘的输入连接
【发布时间】:2017-07-07 11:10:48
【问题描述】:

我做了我的自定义密码视图

class StarsPasswordView : LinearLayout {

    constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) {
        init(context, attrs)
    }

    val passwordHolder = SpannableStringBuilder()

    var count

    fun init(context: Context, attrs: AttributeSet?) {
        orientation = HORIZONTAL
        isFocusable = true
        isFocusableInTouchMode = true
        gravity = Gravity.CENTER

        val attr = context.obtainStyledAttributes(attrs, R.styleable.StarsPasswordView, 0, 0)
        count = attr.getInteger(R.styleable.StarsPasswordView_count, 4)
        attr.recycle()

        drawView(count)

        setOnKeyListener(View.OnKeyListener { v, keyCode, event ->
            if (event.action == KeyEvent.ACTION_DOWN) {
                if (keyCode == KeyEvent.KEYCODE_ENTER)
                    return@OnKeyListener true
                if (keyCode == KeyEvent.KEYCODE_BACK)
                    return@OnKeyListener false
                if (keyCode == KeyEvent.KEYCODE_DEL) {
                    clear()
                    get(0).requestFocus()
                } else
                    if (passwordHolder.length != count) {
                        passwordHolder.append(event.number)
                        val position = passwordHolder.length
                        select(position - 1)

                        if (position < count)
                            get(position).requestFocus()
                        else {
                            passwordFilled?.invoke(passwordHolder.toString())
                        }
                    }
                return@OnKeyListener true
            }
            false
        })
    }

    fun samsungWorkaround() {
        val position = passwordHolder.length
        select(position - 1)

        if (position == count)
            passwordFilled?.invoke(passwordHolder.toString())
        }
    }


    // toggle whether the keyboard is showing when the view is clicked
    override fun onTouchEvent(event: MotionEvent): Boolean {
        if (enableKeyboard && event.action == MotionEvent.ACTION_UP) {
            val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_IMPLICIT_ONLY)
        }
        return true
    }

    override fun onCreateInputConnection(outAttrs: EditorInfo): InputConnection {
        outAttrs.inputType = InputType.TYPE_CLASS_NUMBER
        return InputConnection(this, true)
    }
}

class InputConnection internal constructor(val targetView: StarsPasswordView, fullEditor: Boolean) : BaseInputConnection(targetView, fullEditor) {

    override fun getEditable(): Editable {
        return targetView.passwordHolder
    }
    override fun commitText(text: CharSequence?, newCursorPosition: Int): Boolean {
        val res =  super.commitText(text, newCursorPosition)
        targetView.samsungWorkaround()
        return res
    }
}

要设置的时候问题来了

 outAttrs.inputType = InputType.TYPE_CLASS_NUMBER

在这种情况下

  • InputConnection 不处理数字
  • getEditable() 不会触发
  • commitText() 不会触发

    所以我的解决方法只是在 setOnKeyListener 中处理麻木,如上所示。

有谁知道问题出在哪里?

【问题讨论】:

    标签: android keyboard


    【解决方案1】:

    根据键盘的不同,并非所有键都通过InputConnection 发送。有些像硬键事件一样发送,必须单独处理。这包括标准键盘上的数字键盘数字。据我目前所知,像您一样使用OnKeyListener 是可行的方法(但请参阅@pskink 的cmets here)。

    您可以使用KeyEvent.getUnicodeChar() 将文本字符与控制字符分开,如下所示。

    if (keyEvent.getUnicodeChar() != 0) {
        // unicode text
        char unicodeChar = (char) keyEvent.getUnicodeChar();
    } else {
        // control char
    }
    

    处理您需要的控制代码,但所有有效的 Unicode 字符(包括数字和换行(回车)字符)都会被捕获。

    我在准备回答这个问题时写的问题和答案中更详细地介绍了这方面的一些方面。

    我还更新了my previous answer 关于接收键盘输入的自定义视图。

    【讨论】:

    • 我遇到的一个大问题是键盘事件在三星手机上没有出现。你遇到过这个问题吗?
    • @Gorets,我还没有遇到这个问题,但我没有三星手机可以测试它。那很糟。所以数字,删除和输入不起作用?找到解决方案后请尽快通知我。我也会对此进行更多研究。
    • 这是否描述了您的问题? stackoverflow.com/questions/4282214/…
    • 很少有 SO 问题报告了三星键盘问题,但我没有找到自定义视图的解决方案(没有 EditText 视图)。我会更改我的代码,但它看起来像肮脏的解决方案,所以我把它作为最后一次尝试。继续努力研究。我会让你知道结果。
    • @Gorets,你能在this github project 中测试我的自定义EditText 吗?在demo-app的主活动中,倒数第三项是MongolEditText。如果三星键盘可以使用它,您可以比较我所做的不同之处。如果它不起作用,我们都需要找到不同的解决方案。
    猜你喜欢
    • 1970-01-01
    • 2017-06-22
    • 2014-04-22
    • 1970-01-01
    • 2013-11-21
    • 2016-09-14
    • 2015-10-14
    • 2018-01-23
    • 1970-01-01
    相关资源
    最近更新 更多