在editText上输入特定数量的数字时隐藏键盘

时间:2016-06-15 06:21:11

标签: android-studio

目标: 当在ID_Display上输入6位数时,它会隐藏键盘。

问题: 键盘将无法隐藏,除非按键盘“Enter”按

 public void keyboard_hide_Listener() {
    ID_Display = (EditText) findViewById(R.id.ID_display);
    if (ID_Display != null) {ID_Display.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (event != null && (ID_Display.getText().length()>=6)) { //event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)
                    InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    System.out.println("Edit Text Length: " +ID_Display.getText().length());
                    in.hideSoftInputFromWindow(ID_Display.getApplicationWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

                    return true;

                }
                return false;
            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

(ID_Display.getText().length()>=6

您应该使用v.GetText()代替ID_display