Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: softkeyboard control on HTC desire

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 beautifully on the emulator. Then I came to load it on to my HTC desire for testing and it doesn't work at all. I'm sure it must be because of HTC sense having it's own ime but there must surely be a way to make this work on HTC phones? Anyone else managed to get around this issue?

like image 926
Dream Architect Avatar asked Sep 30 '10 21:09

Dream Architect


3 Answers

I can replicate what I think you are seeing on the HTC Incredible.

Not all soft keyboards will support the IME action button. Some, like the Graffiti soft "keyboard", may have no buttons at all, let alone an IME action button. Even the Compatibility Definition Document says nothing about requiring such an action button for the keyboards supplied with a device.

Hence, you should not rely on the IME action button. If it is there, users can use it. However, always have some other means of accomplishing whatever your goal is.

like image 63
CommonsWare Avatar answered Oct 17 '22 00:10

CommonsWare


I am detecting whether the DONE / GO / RETURN button has been pressed using an onEditorActionListener, but checking for IME options and KeyEvents to cover HTC keyboards as well as any keyboards that accept IME options.

(This code works for HTC Incredible keyboards as well any keyboard that has IME options)

EditText.setOnEditorActionListener(new TextView.OnEditorActionListener(){
    public boolean onEditorAction(TextView exampleView, int actionId, KeyEvent event){
        if(actionId == EditorInfo.IME_ACTION_DONE 
            || actionId == EditorInfo.IME_NULL
            || event.getKeyCode() == KeyEvent.KEYCODE_ENTER){

            //Do something in here
            return true;
        } else {
            return false;
        }
    }
});
like image 21
Asha Avatar answered Oct 16 '22 22:10

Asha


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.

like image 36
Chris Sprague Avatar answered Oct 16 '22 23:10

Chris Sprague