Android: softkeyboard control on HTC desire

后端 未结 3 2134
花落未央
花落未央 2020-12-03 23:08

I wanted a numeric keypad that had a go or done button that closed and executed a calculation class. Thanks to a tip from commonware on where to start I got this working bea

3条回答
  •  天命终不由人
    2020-12-04 00:01

    I was using an EditText with inputType="number" and solved the problem by modifying Asha's solution:

    private TextView.OnEditorActionListener numberEnterListener = new TextView.OnEditorActionListener(){
            public boolean onEditorAction(TextView tv, int actionId, KeyEvent event){
                if(actionId == EditorInfo.IME_ACTION_DONE 
                    || actionId == EditorInfo.IME_NULL
                    || event.getKeyCode() == KeyEvent.KEYCODE_ENTER){
    
                    tv.clearFocus();
    
                    //Stupid keyboard needs to be closed as well
                    InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(tv.getWindowToken(), 0);
    
                    return true;
                } else {
                    return false;
                }
            }
        };
    

    The focus was removed in order to stop showing the number pad. The imm was required because a soft keyboard was still present even after clearing focus.

提交回复
热议问题