片段中的EditText被键盘遮挡

21

编辑:我需要使用键盘,但是它遮住了我的EditText,我需要它滚动以使键盘不再遮挡它。

我正在使用三星平板电脑。

我的样式:

parent="android:Theme.Holo.NoActionBar.Fullscreen"
EditText字段位于可滚动的视图中,如下所示:
片段布局:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_height="match_parent"
         android:layout_width="match_parent"
         android:paddingBottom="@dimen/activity_vertical_margin"
         android:paddingLeft="@dimen/activity_horizontal_margin"
         android:paddingRight="@dimen/activity_horizontal_margin"
         android:paddingTop="@dimen/activity_vertical_margin">

<ScrollView
    android:layout_height="wrap_content"
    android:layout_width="match_parent">

    <LinearLayout
        android:layout_height="match_parent"
        android:layout_marginBottom="20dp"
        android:layout_width="match_parent"
        android:orientation="vertical">

        <TextView
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_width="wrap_content"
            android:text="@string/depot_code"/>

        <EditText
            android:hint="@string/enter_depot_code"
            android:id="@+id/etlocationId"
            android:imeOptions="actionNext"
            android:inputType="number"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_width="wrap_content"
            android:lines="1"
            android:maxLength="12"
            android:singleLine="true">

            <requestFocus/>
        </EditText>

        <TextView
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_width="wrap_content"
            android:text="@string/name"/>

        <EditText
            android:hint="@string/enter_name"
            android:id="@+id/etname"
            android:imeOptions="actionNext"
            android:inputType="textPersonName"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_width="wrap_content"
            android:lines="1"
            android:maxLength="24"
            android:singleLine="true"/>

        <TextView
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_width="wrap_content"
            android:text="@string/street"/>

        <EditText
            android:hint="@string/enter_street"
            android:id="@+id/etstreet"
            android:imeOptions="actionNext"
            android:inputType="textPostalAddress"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_width="wrap_content"
            android:lines="1"
            android:maxLength="24"
            android:singleLine="true"/>

        <TextView
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_width="wrap_content"
            android:text="@string/suburb"/>

        <EditText
            android:hint="@string/enter_suburb"
            android:id="@+id/etsuburb"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_width="wrap_content"
            android:lines="1"
            android:maxLength="24"
            android:singleLine="true"/>

        <TextView
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_width="wrap_content"
            android:text="@string/state"/>

        <Spinner
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:id="@+id/spinner"
            android:imeOptions="actionNext"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_width="fill_parent"
            android:spinnerMode="dropdown"/>

        <TextView
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_width="wrap_content"
            android:text="@string/phone"/>

        <EditText
            android:hint="@string/enter_phone"
            android:id="@+id/etphone"
            android:imeOptions="actionDone"
            android:inputType="phone"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_width="wrap_content"
            android:lines="1"
            android:maxLength="12"
            android:singleLine="true"/>

        <Button
            android:id="@+id/btnadd"
            android:layout_gravity="center"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_width="wrap_content"
            android:text="@string/add"/>
    </LinearLayout>
</ScrollView>
</FrameLayout>

活动布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/tech_controller"
          android:layout_height="match_parent"
          android:layout_width="match_parent"
          android:orientation="vertical"
          android:paddingBottom="@dimen/activity_vertical_margin"
          android:paddingLeft="@dimen/activity_horizontal_margin"
          android:paddingRight="@dimen/activity_horizontal_margin"
          android:paddingTop="@dimen/activity_vertical_margin">

< .../ Layouts. ../>

<FrameLayout
    android:id="@+id/second"
    android:layout_height="match_parent"
    android:layout_width="fill_parent"/>
</LinearLayout>

有类似的问题被问到过,但我没有找到一个有效的答案。

我的项目中有很多片段与一个活动连接在一起。在活动的清单中,我有:

android:windowSoftInputMode="stateHidden"

其中一些片段需要用户输入。

在片段布局中,我使用:

每个片段嵌套如下:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent" .../>

<ScrollView
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <LinearLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:orientation="vertical">

        <EditText
            android:imeOptions="actionNext"
            or
            android:imeOptions="actionDone" .../>

除了键盘会遮挡页面下方的EditTexts,这段代码效果很好。

enter image description here

我已经反复阅读过以下内容:

http://developer.android.com/reference/android/widget/TextView.html#attr_android:imeOptions

尝试了这些建议,但是当调用活动时键盘仍然可见,而我不想要这个。它也不起作用。也许因为它是一个片段?

SoftKeyboard hiding EditText

Android Keyboard hides EditText

我一直在尝试找到一种动态解决方案。感谢任何帮助。

编辑:这是用于平板电脑的,并且只有一种键盘选项,带有语言更改。


编辑:我的解决方案

我通过安装另一个软键盘来解决它。https://code.google.com/p/softkeyboard/wiki/HowTo


你能提供一张截图吗? - petey
1
你使用的输入法有自己的“浮动”布局,你可以尝试使用内置的输入法,如谷歌键盘,或将你的输入法设置为正常的全宽布局来测试你的代码吗? - Yenchi
@Heyyou 长按键盘上的空格键。这将弹出一个对话框,其中您可以选择另一个键盘。 - petey
1
@嘿,你可以尝试进入设置 > 语言和输入 > 查找键盘。 - petey
你是否使用getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);标志或其他标志来全屏显示你的活动? - Lalit Poptani
尝试在onCreate()中清除标志,使用getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); - Lalit Poptani
9个回答

8

请尽量只使用WindowsSoftInputMode作为adjustpan

例如:

android:windowSoftInputMode="adjustPan" 

5
尝试在您的片段的onActivityCreated()函数中调用InputMethodManager.hideSoftInputFromWindow(),参见此SO答案
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    AppUtil.hideKeyboard(getActivity(), getView());
}

hideKeyboard() 看起来像这样

public static void hideKeyboard(Activity activity, View viewToHide) {
    InputMethodManager imm = (InputMethodManager) activity
            .getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(viewToHide.getWindowToken(), 0);
}

4
你可以在fragment的onActivityCreated方法中尝试将这段代码放入:
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

当您启动带有片段的活动时,不应使键盘出现。


尝试在你的Android清单文件中添加以下内容,除了我的先前回答之外: android:windowSoftInputMode="adjustResize" 我认为它会起作用 :) - Joey
2
你也可以尝试这个: getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); - Joey
你好 @Heyyou,我猜可能是ScrollView和软键盘有冲突 :) 无论如何,希望你能找到解决方案。当我再次找到解决方案时,我会回到这里的 ^_^ - Joey
让我们在聊天中继续这个讨论 - Joey

4

我尝试了这段代码,并解决了我的问题。

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

2
我通过在Google Play商店安装另一个软键盘解决了这个问题。正是Petey的评论让我探索了这个解决方案。

您使用的输入法有自己的“浮动”布局,您可以尝试使用Google键盘等内置输入法或将您的输入法设置为普通的全宽布局。

所以我想,无论如何调整视图布局都不会影响具有独立布局的软键盘。
我使用了这个链接 https://code.google.com/p/softkeyboard/wiki/HowTo 它甚至可以与Android清单一起使用:
android:windowSoftInputMode="stateHidden"

我使用imeoptions来控制编辑器光标在视图中的流动。
编辑更新:
我刚刚在一款无品牌的安卓平板上尝试了这个应用程序,它配备了一个通用的安卓键盘,而且它可以正常工作,无需进行任何更改。似乎三星默认键盘很痛苦。

1
将此更改为LinearLayout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

(和闭合标签)。注意方向。

更新清单,删除此内容:

<application ... >
    <activity
      android:windowSoftInputMode="stateVisible|adjustResize|adjustPan">
        ...
    </activity>
    ...
</application>

改变这个(高度):
<ScrollView
    android:layout_height="wrap_content"
    android:layout_width="match_parent">

@Hey你能否添加完整的XML代码。我对父级布局很感兴趣。 - xyz
@Heyyou,我在新项目中尝试使用了你的线性布局XML,并且它完美地运行了。我进行了一次修改,请尝试从清单中删除它。 - xyz
让我们在聊天中继续这个讨论 - xyz

0
请记住,使用getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)无法隐藏键盘。

0
在显示您的Snackbar小部件之前,请像这样隐藏键盘,
public void showSnackBar(){ 
InputMethodManager manager = (InputMethodManager)getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
manager.hideSoftInputFromWindow(getView().getWindowToken(),0); 
Snackbar snackbar = Snackbar.make(getView(), "Some message!", Snackbar.LENGTH_INDEFINITE).show(); 
}

-1
我的做法是在FrameLayout中与您一样使用ScrollView,并添加了此属性:android:windowSoftInputMode="adjustPan"。最后,我的代码如下所示,已经能够正常运行。我不需要进行任何编程方面的更改:
 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ProductFragment"
    android:windowSoftInputMode="adjustPan">

    <ScrollView
        android:layout_height="wrap_content"
        android:layout_width="match_parent">
      ...
    </ScrollView>
</FrameLayout>

android:windowSoftInputMode?这是绝对错误的。 - TeeTracker

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