【问题标题】:Android avoid keyboard covering Edit Text programmaticallyAndroid避免键盘覆盖以编程方式编辑文本
【发布时间】:2017-10-28 06:17:46
【问题描述】:

我编写了以下通用的、每次可用的函数来显示和关闭键盘:

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

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。

【问题讨论】:

  • 可以放你的xml代码吗?
  • 我正在添加相对布局并以编程方式和动态方式添加编辑文本。所以没有 XML。
  • 整个 xml ?
  • @MehulKanzariya :我在您的链接中尝试了所有方法并帮助我。我相应地编辑了我的问题。

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


【解决方案1】:

您需要将 EditText 包裹在 LinearLayout 中,然后用 ScrollView 包裹它。如果你通过代码来做会有点困难,所以我的建议是改变你的 xml 布局。

【讨论】:

  • 正如我在问题中提到的,ScrollView 似乎是一种解决方法,但不是解决问题的方法。添加滚动视图会导致其他布局问题。我的编辑文本在相对布局内,可以在屏幕上移动。
【解决方案2】:

您的根布局

<?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"/>

【讨论】:

  • 您的代码对我没有帮助。原因 1) ScrollView 只能在子视图上托管。 2)编辑文本应该在相对布局内,因为它可以移动。
猜你喜欢
  • 1970-01-01
  • 2017-09-05
  • 1970-01-01
  • 2018-01-06
  • 1970-01-01
  • 2014-12-10
  • 1970-01-01
  • 1970-01-01
  • 2021-03-09
相关资源
最近更新 更多