开发者

Softkeyboard not shown when EditText is selected

I have a Service (implementing TextWatcher) that creates a View at the top of the screen, which only contains an EditText field. This "top bar" is then displayed on top of all activities. At the moment, the EditText is displayed, and TextWatcher methods are called correctly if I set its content programmatically.

However the desired behaviour is that, when the EditText is selected, a softkeyboard should pop up from the bottom of the screen but this is not happening, i.e. no keyboard is displayed.

I can't figure out why it works with Activities but not with my Service. My gut feeling is that the keyboard wants to be displayed in the same view of the TextWatcher, not sure though.

Can anyone please help?

This is a snippet of my code:

public final class TopBarService extends Service implements TextWatcher, OnFocusChangeListener {
    protected LayoutInflater mInflater;
    protected WindowManager mWindowManager;
    protected View mTopEditBar;
    protected EditText mTopEditText;

    public void onCreate() {
        // Create top bar
        mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        mTopBar = mInflater.inflate(R.layout.top_bar,开发者_开发百科 null);
        mTopEditText = (EditText) mTopBar.findViewById(R.id.top_edit_text);
        WindowManager.LayoutParams params = ...; // some "standard" params here
        mWindowManager.addView(mTopBar, params);

        mTopEditText.addTextChangedListener(this);
        mTopEditText.setOnFocusChangeListener(this);
        mTopEditText.setInputType(InputType.TYPE_CLASS_TEXT);
        mTopEditText.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);
    }

    // TextWatcher implementation
    public void afterTextChanged(Editable s) {
        //...
    }

    public void beforeTextChanged(Editable s) {
        //...
    }

    public void onTextChanged(Editable s) {
        //...
    }

    public void onFocusChange(View v, boolean hasFocus) {
        // see Edit below
    }

    ...
}

Where res/layout/top_bar.xml is:

<EditText
    android:id="@+id/top_edit_text"
    android:layout_centerVertical="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
</EditText>

Edit: Little progress.

I've noticed that, by using the InputMethodManager, this is not active:

public void onFocusChange(View v, boolean hasFocus) {
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.restartInput(mTopEditText);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    imm.showSoftInput(mTopEditText, InputMethodManager.SHOW_IMPLICIT);

    Log.d(TAG, "active? " + imm.isActive()); // false
    Log.d(TAG, "activeOnView? " + imm.isActive(mTopEditText)); // false
    Log.v(TAG, "accepting text? " + imm.isAcceptingText()); // false
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜