选择EditText时未显示软键盘

时间:2011-07-11 15:04:23

标签: android android-layout

我有一个Service(实现TextWatcher),它在屏幕顶部创建一个View,它只包含一个EditText字段。然后在所有活动的顶部显示这个“顶栏”。目前,如果我以编程方式设置其内容,则会显示EditText,并正确调用TextWatcher方法。

然而,所需的行为是,当选择EditText时,应从屏幕底部弹出一个软键盘,但这不会发生,即不显示键盘。

我无法弄清楚为什么它适用于活动而不适用于我的服务。我的直觉是键盘想要显示在TextWatcher的相同视图中,但不确定。

有人可以帮忙吗?

这是我的代码片段:

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
    }

    ...
}

res / layout / top_bar.xml是:

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

编辑:进展甚微。

我注意到,通过使用InputMethodManager,它不活动:

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 个答案:

没有答案