显示软键盘后如何防止窗口弹出位移?

时间:2013-07-27 06:26:11

标签: android android-softkeyboard

我用edittext创建了WindowPopup。当我专注于它时,软键盘显示并将此弹出窗口置于顶部边界之上,因此我无法看到我正在键入的内容。我想在没有任何视图位移的情况下显示键盘,就在它们上方。 我读到我可以为它更改softInputMode,所以我创建了一个从EditText扩展的类,并尝试在onFocusListener中更改inputMode,但它没有帮助。

setOnFocusChangeListener(new OnFocusChangeListener() {

    @Override
    public void onFocusChange(View veiw, boolean has_focus) {

        if (has_focus) {
            //Try to change input mode to prevent displacing
            ((Activity) getContext()).getWindow().setSoftInputMode(
            WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
        } else {
            //.. return back previous input mode
        }

});

我这样做了,因为我只在这个弹出窗口中需要这样的行为,但我甚至尝试在我的苔藓文件中更改动作属性

android:windowSoftInputMode="adjustNothing"

android:windowSoftInputMode="stateHidden"

我可以在不移动任何视图的情况下显示键盘吗?

P.S。我正在使用Android API 15

1 个答案:

答案 0 :(得分:2)

当PopupWindow创建弹出视图时,它会使用softInputMode设置新的WindowManager.LayoutParams,它会覆盖Window.softInputMode的行为。这是PopupWindow的代码片段

private WindowManager.LayoutParams createPopupLayout(IBinder token) {

    WindowManager.LayoutParams p = new WindowManager.LayoutParams();
    p.gravity = Gravity.LEFT | Gravity.TOP;
    p.width = mLastWidth = mWidth;
    p.height = mLastHeight = mHeight;
    if (mBackground != null) {
        p.format = mBackground.getOpacity();
    } else {
        p.format = PixelFormat.TRANSLUCENT;
    }
    p.flags = computeFlags(p.flags);
    p.type = mWindowLayoutType;
    p.token = token;
    /*mSoftInputMode is the private field which is by default equals
      to WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED 
    */
    p.softInputMode = mSoftInputMode;
}

因此,要更改softInputMode,您只需要调用PopupWindow的公共方法

setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);

并且无需记住以前的软输入法,因为此行为仅适用于此PopupWindow