views:

203

answers:

2

Hi,

I have an Activity with an EditText, a button and a ListView. The purpose is to type a search screen in the EditText, press the button and have the search results populate this list.

This is all working perfectly, but the virtual keyboard is behaving strange. If I click the EditText, I get the virtual keyboard. If I click the "Done" button on the virtual keyboard, it goes away. However, if I click my search button before clicking "Done" on the virtual keyboard, the virtual keyboard stays and I can't get rid of it. Clicking the "Done" button does not close the keyboard. It changes the "Done" button from "Done" to an arrow and remains visible.

Thanks for your help

A: 

You should implement OnEditorActionListener for your EditView

public void performClickOnDone(EditView editView, final View button){
    textView.setOnEditorActionListener(new OnEditorActionListener() {

        @Override
        public boolean onEditorAction(EditView v, int actionId, KeyEvent event) {
            hideKeyboard();
            button.requestFocus();
            button.performClick();
            return true;
        }
    });

And you hide keyboard by:

public void hideKeybord(View view) {
    inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

You should also fire keyboard hiding in your button using onClickListener

Now clicking 'Done' on virtual keyboard and button will do the same - hide keyboard and perform click action.

pixel
Excellent, but I'm not quite following. I think the post ate some of the code (there's nothing after "public void" in your sample). I attempted to setOnEditorActionListner in my Activity's onCreate method, but it doesn't know what setOnEditorActionListener is. I get an "Anonymous inner type" notification. (I'm doing this in my Activity onCreate method) http://i37.tinypic.com/6ozkig.png
Andrew
It looks like there are a couple of mistakes in this code, but it's the right idea. For one thing, the OnEditorActionListener interface is an inner class, so you need to either import it explicitly (Eclipse won't do it for you in this case) or refer to it as `TextView.OnEditorActionListener`.
MatrixFrog
I'm having a bit of trouble. I've implemented onEditorActionListener (public class SearchActivity extends ListActivity implements OnClickListener, OnEditorActionListener), I've attached a listener to my EditText (mSearchText.setOnEditorActionListener(this);), but Eclipse wont allow me to override the onEditorAction handler (public boolean onEditorAction(TextView v, int actionId, KeyEvent event)). It says it must override a superclass method. Any ideas?
Andrew
Hi, you can inline your OnEditorActionListener by typing yourEditView.setOnEditorActionListener(new OnEditorActionListener() {....
pixel
A: 
    mMyTextView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                // hide virtual keyboard
                InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(m_txtSearchText.getWindowToken(), 0);
                return true;
            }
            return false;
        }
    });
Andrew