views:

597

answers:

3

Hello,

I have used a bit of Android code to override the "Done" button in my EditText field:

   myEditField.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE) {

                mySubroutine();

                return true;
            }
            return false;
        }
    });

Activating the field calls up the keyboard, and pressing "Done" evaluates mySubroutine() successfully. However, the keyboard no longer goes away when I press "Done". How do I restore this default behaviour to the routine?

+2  A: 

You can close the keyboard by doing:

InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindowToken(), 0);
Macarse
I'm using Eclipse and I'm getting the two following errors: "InputMethodManager cannot be resolved to a type"and "the method getWindowToken() is undefined for the type new TextView.onEditorActionListener(){}"I'm not much of a Java programmer so I don't know how to interpret these. Do I need to include something earlier?
Marshall Ward
Try pressing Control+Shift+o to do the corresponding imports.
Macarse
Thanks, there was a dependency that I needed.I also had to replace getWindowToken() with v.getWindowToken() but otherwise it works great, thanks! Now I just need to understand what it did
Marshall Ward
A: 

Why not:

myEditField.setOnEditorActionListener(new TextView.OnEditorActionListener() { 
    @Override 
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
        if (actionId == EditorInfo.IME_ACTION_DONE) { 

            mySubroutine(); 
        } 
        return false; 
    } 
}); 

Just return false after you handle your code.

just_another_coder
A: 
u can write this code in button's click listener,here editview ,u must write u'r editview's object


InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editview.getWindowToken(), 0);
nish