How can I tell if the input method picker is open or closed?

前端 未结 3 1905
孤城傲影
孤城傲影 2020-12-18 22:12

My app opens the input method picker (the menu where you choose a keyboard) with InputMethodManager.showInputMethodPicker(). My app doesn\'t actually create the

3条回答
  •  情书的邮戳
    2020-12-18 23:05

    Here is a small trick. Please do test it and let us know if it works.

    All you have to do is make your activity extend this InputMethodActivity. When you need the user to pick input method, call pickInput(), and onInputMethodPicked() will be called when the user is done.

    package mobi.sherif.inputchangecheck;
    
    import android.os.Bundle;
    import android.os.Handler;
    import android.content.Context;
    import android.support.v4.app.FragmentActivity;
    import android.view.inputmethod.InputMethodManager;
    
    public abstract class InputMethodActivity extends FragmentActivity {
        protected abstract void onInputMethodPicked();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            mState = NONE;
            super.onCreate(savedInstanceState);
        }
    
        @Override
        public void onWindowFocusChanged(boolean hasFocus) {
            super.onWindowFocusChanged(hasFocus);
            if(mState == PICKING) {
                mState = CHOSEN;
            }
            else if(mState == CHOSEN) {
                onInputMethodPicked();
    
            }
        }
    
        private static final int NONE = 0;
        private static final int PICKING = 1;
        private static final int CHOSEN = 2;
        private int mState;
        protected final void pickInput() {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showInputMethodPicker();
            mState = PICKING;
        }
    }
    

提交回复
热议问题