如何使删除按钮从edittext移动到上一个

时间:2017-02-08 08:27:03

标签: android android-edittext textwatcher

所以我的AndroidStudio项目有点问题。 假设我有3 EditText s。每个字母的最大长度为1个字母,当我输入时,它会自动移动到下一个字母。问题是,一旦我按下删除按钮后退,它什么都不做。我使用了TextWatcher和所有内容,但是当我使用密钥监听器时,它要么没有做任何事情要么删除所有内容。

1 个答案:

答案 0 :(得分:0)

我认为设置关键侦听器将有助于检查删除事件是否为真并在那里处理它,而不是使用 TextWatcher。它仍然需要修复,因为当您继续按下删除按钮时,它会跳转到前一个 EditText 但不会恢复删除过程。例如,在 pin 码输入视图的情况下,将每个 EditText 的 maxLength 设置为 1。

private var BOX_SIZE : Int = 1
private var mIsLastBoxReached : Boolean = false

editText_first_box.setOnKeyListener { view, i, keyEvent ->
    if(editText_first_box.length() == BOX_SIZE && keyEvent.keyCode != KeyEvent.KEYCODE_DEL){
        editText_second_box.isEnabled = true
        editText_second_box.requestFocus()
    }
    false
}

editText_second_box.setOnKeyListener { view, i, keyEvent ->
    if(keyEvent.keyCode == KeyEvent.KEYCODE_DEL && editText_second_box.length() == 0){
        editText_first_box.requestFocus()
        editText_second_box.isEnabled = false
    }else if(editText_second_box.length() == BOX_SIZE && keyEvent.keyCode != KeyEvent.KEYCODE_DEL){
        editText_third_box.isEnabled = true
        editText_third_box.requestFocus()
    }
    false
}

editText_third_box.setOnKeyListener { view, i, keyEvent ->
    if(keyEvent.keyCode == KeyEvent.KEYCODE_DEL && editText_third_box.length() == 0){
        editText_second_box.requestFocus()
        editText_third_box.isEnabled = false
    }else if(editText_third_box.length() == BOX_SIZE && keyEvent.keyCode != KeyEvent.KEYCODE_DEL){
        editText_fourth_box.isEnabled = true
        editText_fourth_box.requestFocus()
    }
    false
}

editText_fourth_box.setOnKeyListener { view, i, keyEvent ->
    if(keyEvent.keyCode == KeyEvent.KEYCODE_DEL && editText_fourth_box.length() == 0){
        editText_third_box.requestFocus()
        editText_fourth_box.isEnabled = false
        mIsLastBoxReached = false
    }else if(editText_third_box.length() == BOX_SIZE && keyEvent.keyCode != KeyEvent.KEYCODE_DEL){
        //TODO what to do if card number inserted
        mIsLastBoxReached = true
    }
    false
}