Input connection for numeric type keyboard

前端 未结 1 1291
庸人自扰
庸人自扰 2021-01-24 05:53

I did my custom pin-code view

class StarsPasswordView : LinearLayout {

    constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, at         


        
相关标签:
1条回答
  • 2021-01-24 06:36

    Depending on the keyboard, not all keys are sent through the InputConnection. Some are sent like hard keys events and must be processed separately. This includes the number pad numbers on the standard keyboard. To the best of my current knowledge, using the OnKeyListener like you did is the way to go (but see @pskink's comments here).

    You can separate the text characters from the control characters with KeyEvent.getUnicodeChar() as is shown below.

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

    Handle the control codes that you need, but all the valid Unicode characters (including numbers and the new line (enter) character) will be caught.

    I cover some aspects of this in more detail in the question and answers I wrote while preparing to answer this question.

    • Differentiating text keycode from control keycode in Android KeyEvent
    • Need table of key codes for android and presenter

    I also updated my previous answer about custom views receiving keyboard input.

    0 讨论(0)
提交回复
热议问题