手动使用InputMethodManager显示软键盘不会调整窗口位置

3

我有一个使用 windowSoftInputMode="adjustPan" 的 Activity,并且我有一个 EditTextOnPreDrawListener,调用以下内容:

editText.requestFocus();
inputManager.showSoftInput(editText, 0);

这段代码可以正常工作,当用户点击EditText时,它会推动Activity以腾出空间来打开键盘。然而,如果我使用返回按钮关闭键盘(它会将窗口平移回原始位置),然后再次点击EditText打开键盘,键盘会弹出,但窗口不会自动调整。

我甚至试图为EditText添加一个OnClickListener并再次调用同样的两个方法:

editText.requestFocus();
inputManager.showSoftInput(editText, 0);

但是直到我关闭窗口并重新打开窗口之前,窗口才不会滚动。有什么建议吗?

你尝试在onBackPressed中调用hideSoftInputFromWindow了吗?这是一个小技巧,可以帮助你达到想要的效果。 - Naveen
很不幸的是,似乎没有任何效果。 - Kevin Coppock
2个回答

0

所以这并不是问题的确切“解决方案”,但这是我最终采用的解决方法。我创建了一个LinearLayout的子类,在IME接收到后退按钮之前拦截它:

public class IMEInterceptLinearLayout extends LinearLayout {
    //For some reason, the event seems to occur twice for every back press
    //so track state to avoid firing multiple times
    private boolean notifiedListener = false;
    private OnBackPressedPreIMEListener listener;

    public IMEInterceptLinearLayout(Context context) {
        super(context);
    }

    public IMEInterceptLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public IMEInterceptLinearLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void setOnBackPressedPreIMEListener(OnBackPressedPreIMEListener listener) {
        this.listener = listener;
    }

    private void fireOnBackPressedPreIME() {
        if(listener != null && !notifiedListener) {
            listener.onBackPressedPreIME();
            notifiedListener = true;
        }
    }

    @Override
    public boolean dispatchKeyEventPreIme(KeyEvent event) {
        if(event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            fireOnBackPressedPreIME();
            return true;
        } else return super.dispatchKeyEventPreIme(event);
    }

    public interface OnBackPressedPreIMEListener {
        public void onBackPressedPreIME();
    }
}

然后我只需将我的窗口注册为布局的监听器,并在接收到事件时关闭窗口和键盘,禁止在窗口可见时关闭键盘。


0

我认为这三个步骤可以解决你的问题。

1)在清单文件中将windowSoftInputMode="adjustPan"更改为windowSoftInputMode="adjustResize"

2)你正在使用的EditText布局,将父布局更改为ScrollView。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <EditText
        android:id="@+id/edittextview"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:inputType="textFilter"
        android:padding="10dp"
        android:imeOptions="actionDone"
        android:scrollbars="vertical"
        android:textSize="14sp" />
</ScrollView>

3) 为了明确地显示键盘,以避免“按下返回按钮时键盘消失且窗口未调整大小”,在EditText的点击事件中编写以下代码:

edittext.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                InputMethodManager m = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (m != null) {
                    m.toggleSoftInput(0, InputMethodManager.SHOW_IMPLICIT);
                    edittext.requestFocus();
                }
            }
        });

希望这能解决你的问题。


谢谢,我明天会尝试第三个选项;但更改为ScrollViewadjustResize不是期望的行为;它需要平移而不是调整窗口大小。 - Kevin Coppock
不幸的是,它没有帮助,同样的问题仍然存在。 - Kevin Coppock

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接