EdiText with numeric InputType does not accept keyboard input

67 Views Asked by At

I created a class that in a linear layout creates 6 edittext that are managed with this method, but it only works if I remove the inputtype, if I try to limit it to numbers it doesn't accept any input. Ideas?

    private fun createAndAddChildInLayout(index: Int, context: Context): View? {
    val editext = EditText(context)
    editext.maxWidth = 1
    editext.tag = index
    editext.gravity = Gravity.CENTER
    editext.setTextColor(context.getColor(R.color.black))
    editext.textSize = 20f
    editext.keyListener = DigitsKeyListener.getInstance("0123456789")
    editext.inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_SIGNED
    val param = LayoutParams(10, LayoutParams.WRAP_CONTENT, 1f)
    editext.layoutParams = param
    editext.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) {
            currentIndex = editext.tag.toString().toInt()
            if (editext.text.toString().length == 1 && !disableTextWatcher) {
                getNextEditText(currentIndex)
            } else if (editext.text.isEmpty() && !disableTextWatcher) { // && !isFirstTimeGetFocused && !backKeySet) {
                getPreviousEditText(currentIndex)
            }
        }

        override fun afterTextChanged(s: Editable) {}
    }.also { txtWatcher = it })
    editext.setOnKeyListener { v, keyCode, event ->
        if (keyCode == KeyEvent.KEYCODE_DEL) {
            currentIndex = editext.tag.toString().toInt()
            if (editext.text.isEmpty() && !disableTextWatcher) {
                getPreviousEditTextFocus(currentIndex)
            } else {
                disableTextWatcher = true
                editext.setText("")
                disableTextWatcher = false
            }
            backKeySet = true
        }
        true
    }
    editext.setOnEditorActionListener { v, actionId, event ->
        if (event != null && event.keyCode == KeyEvent.KEYCODE_ENTER || actionId == EditorInfo.IME_ACTION_DONE) {
            if (currentIndex == entryCount - 1 && getEnteredText()!!.length == entryCount) {
                mListener?.onFinish(getEnteredText())
            }
        }
        false
    }
    return editext
}
0

There are 0 best solutions below