Android以编程方式避免键盘覆盖编辑文本

时间:2017-10-28 06:17:47

标签: java android android-edittext keyboard android-softkeyboard

我编写了以下编码,每次可用的功能来显示和关闭键盘:

| == |以编程方式显示和关闭键盘:

InputMethodManager iptKeybodMgrVar;

void keybodDspFnc(EditText txtEdtVyuVar)
{
    txtEdtVyuVar.requestFocus();
    if (iptKeybodMgrVar == null)
    {
        iptKeybodMgrVar = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    }
    iptKeybodMgrVar.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
}

void keybodDsmFnc(EditText txtEdtVyuVar)
{
    iptKeybodMgrVar.hideSoftInputFromWindow(txtEdtVyuVar.getWindowToken(), 0);
}

但问题是,当编辑文本位于键盘下方时,活动不会向上移动。它仅在用户开始输入时才会移动。

那么一旦键盘出现,我该如何让它向上移动?

我通过以下所有链接,没有任何帮助我:

Android: How do I prevent the soft keyboard from pushing my view up?

adjustPan not preventing keyboard from covering EditText

Android : Soft Keyboard covering edit text up (adjustresize or adjustpan doesnt work)

https://readyandroid.wordpress.com/android-soft-keyboard-covers-edittext-field-overscroll-to-soft-keyboard/

我尝试过并且没有工作:

android:windowSoftInputMode="adjustPan|stateAlwaysHidden" 

android:windowSoftInputMode="adjustResize|stateAlwaysHidden" 

android:windowSoftInputMode="adjustResize|adjustPan" 

还试过这段代码:

(所以,不重复:) Move layouts up when soft keyboard is shown?

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

//This Code brings up the keyboard at starting of the activity and pushes all Edit Text up. So not useful 
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE|
            WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

我可以添加ScrollView,但它似乎只是解决方法而且似乎不是解决方案。

感觉应该以编程方式设置解决方案,例如设置InputMethodManager。

2 个答案:

答案 0 :(得分:0)

您需要将 EditText 包装在 LinearLayout 中,然后使用 ScrollView 将其换行。如果你通过代码做的话有点难,所以我的建议是改变你的xml布局。

答案 1 :(得分:0)

您的根布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/root"
android:layout_height="match_parent">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <!--extra views-->


    <!--add below layout programmatically-->
    <!--<include layout="@layout/layout_dynamic"/>-->


    </ScrollView>
</RelativeLayout>

动态布局

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dynamicLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<EditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="text" />

</LinearLayout>

动态添加视图(无需指定行为)

private void addEditText(){
    RelativeLayout mRootLayout = (RelativeLayout) findViewById(R.id.root);

    LayoutInflater inflater = LayoutInflater.from(mActivity);
    View newView = inflater.inflate(R.layout.layout_dynamic,mRootLayout);
    EditText editText = (EditText) newView.findViewById(R.id.editText);

    mRootLayout.addView(newView);


}

清单文件

 <activity
        android:name=".MainActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
        android:windowSoftInputMode="adjustPan"/>