Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
530 views
in Technique[技术] by (71.8m points)

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

My app opens the input method picker (the menu where you choose a keyboard) with InputMethodManager.showInputMethodPicker(). My app doesn't actually create the picker (it's created by InputMethodManager) but I know it's a ContextMenu and its id is R.id.switchInputMethod.

enter image description here

The picker is part of a multi-step wizard so I need to know when the picker closes so my app can proceed to the next step. Right now I'm checking in a background thread if the default keyboard changed, but that doesn't help if the user selects the same keyboard or presses back.

So I need a way to tell when the picker closes (or some other clever way to know when to proceed).

Thanks in advance...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...