线性布局内的滚动视图导致出现软键盘

scrollview within linearlayout causing soft keyboard to appear

我有一个包含许多 edittext 字段的 linerlayout。这一切都在纵向模式下按预期工作,特别是软键盘只有在点击一个编辑文本字段后才会出现。

但是,当设备旋转到横向模式时,底部的几个编辑文本从屏幕底部消失,无法访问它们。

解决方案看起来很简单,在最顶层的线性布局下添加一个滚动视图,并创建一个线性布局来包含所有的编辑文本。

这解决了滚动问题,但现在第一个编辑文本会自动获得焦点并弹出软键盘。

我试过将焦点设置到隐藏的文本视图并明确隐藏键盘,但没有什么能阻止显示键盘。

这是(大部分)有问题的布局 xmal 文件:-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent" 
    android:orientation="vertical"
    android:showDividers="none"
    android:background="#ffffff"
    android:paddingTop="10dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/scrollView" >

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

        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:stretchColumns="*">

...

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Subject"
            android:id="@+id/textView3"
            android:textStyle="bold" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/subject"
            android:editable="false"
            android:ellipsize="end"
            android:singleLine="true"
            android:textStyle="bold"
            android:inputType="textCapWords"
            android:background="@drawable/apptheme_edit_text_holo_light" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Event Text"
            android:id="@+id/textView4"
            android:textStyle="bold" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textMultiLine|textCapSentences"
            android:id="@+id/text"
            android:linksClickable="false"
            android:enabled="true"
            android:editable="true"
            android:capitalize="sentences"
            android:nestedScrollingEnabled="true"
            android:minLines="3"
            android:gravity="top"
            android:background="@drawable/apptheme_edit_text_holo_light" />

...

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Additional Comments"
            android:id="@+id/textView6"
            android:textStyle="bold" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textMultiLine|textCapSentences"
            android:id="@+id/comments"
            android:minLines="3"
            android:gravity="top"
            android:editable="true"
            android:nestedScrollingEnabled="true"
            android:textAlignment="gravity"
            android:background="@drawable/apptheme_edit_text_holo_light" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/id"
            android:visibility="gone" />
    </LinearLayout>
</ScrollView>

任何人都可以告诉我我做错了什么,或者如何在点击编辑文本之前抑制键盘。

非常感谢

将此添加到您遇到问题的 activity 标签(Android 清单中):

android:windowSoftInputMode="stateHidden"

您可以像这样以编程方式执行此操作:

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

为您的 ScrollView 设置这些属性:

android:focusable="true"
android:focusableInTouchMode="true"

您是否在清单中尝试过 activity:

     <activity
        android:name=".YourActivity"
        android:configChanges="orientation"
        android:label="@string/app_name"
        android:launchMode="singleTask"
        android:screenOrientation="landscape"
        android:windowSoftInputMode="stateHidden|adjustResize" >
    </activity>