如何防止键盘遮挡我的EditText?

3
我有一个RelativeLayout置于顶部,紧接着在中心我有一个ListView, 最后在底部我有另一个RelativeLayout,里面有EditText和Button。
我希望当我点击EditText并且输入法(虚拟键盘)出现时,ListView可以自适应大小。如果我在manifest中加入adjustResize,那么ListView将会被调整大小以腾出空间留给输入法,但是下面的RelativeLayout会被输入法遮挡,无法看到我正在输入的内容。如果我加入adjustPan,那么输入法会将所有内容上推,ListView不会被调整大小,而我也失去了顶部的RelativeLayout。
我的代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/application_background"
    android:paddingLeft="15px"
    android:paddingRight="15px"
    android:paddingTop="15px"
    android:paddingBottom="15px">

    <RelativeLayout
        android:id="@+id/rlyParentPost"
        android:orientation="vertical"
        android:layout_width="450px"
        android:layout_height="85px"
        android:background="@drawable/app_gradient_color"
        android:layout_centerHorizontal="true">
        <ImageView
            android:id="@+id/imgUserOfParent"
            android:layout_marginLeft="10px"
            android:layout_marginTop="10px"
            android:background="@drawable/userfeed"
            android:layout_width="64px"
            android:layout_height="64px"
            android:layout_below="@+id/logoImg">
        </ImageView>
        <TextView
            android:layout_below="@+id/logoImg"
            android:layout_toRightOf="@+id/imgUserOfParent"
            android:id="@+id/lblNameOfParent"
            android:textSize="17px"
            android:textStyle="bold"
            android:layout_marginLeft="5dip"
            android:layout_marginTop="11dip"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#ffffff">
        </TextView>
        <TextView
            android:layout_below="@+id/lblNameOfParent"
            android:layout_toRightOf="@+id/imgUserOfParent"
            android:id="@+id/lblBodyOfParent"
            android:textColor="#ffffff"
            android:textSize="17px"
            android:layout_marginLeft="5dip"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </TextView>
        <TextView
            android:layout_below="@+id/lblBodyOfParent"
            android:layout_toRightOf="@+id/imgUserOfParent"
            android:id="@+id/lblDateOfParent"
            android:textColor="#70ccff"
            android:textSize="11px"
            android:layout_marginLeft="7dip"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </TextView>
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/rlyParentPost"
        android:id="@+id/contentLayout">

     <ListView
       android:isScrollContainer="true"
         android:id="@+id/lsvComments"
         android:layout_height="515px"
         android:layout_width="450px"
         android:background="#ffffff"
         android:listSelector="@drawable/multi_white_color"
         android:drawSelectorOnTop="false"
         android:divider="#9aa5ac"
         android:cacheColorHint="#00000000"
         android:dividerHeight="1px">
     </ListView>

     <RelativeLayout
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:padding="5dip"
         android:layout_below="@+id/lsvComments"
         android:background="@drawable/app_gradient_comments"
         android:id="@+id/contentLayout">
         <EditText
             android:inputType="text"
             android:id="@+id/txtComment"
             android:hint="What are you working on?" 
             android:textSize="22px"
             android:layout_marginLeft="8px"
             android:layout_height="72px"
             android:layout_width="344px">
         </EditText>
         <Button
             android:layout_toRightOf="@+id/txtComment"
             android:id="@+id/cmdShare"
             android:layout_width="79px"
             android:layout_height="65px"
             android:background="@drawable/feed_comments_multi_stage"
             android:layout_marginLeft="7px">
         </Button>
    </RelativeLayout> 
   </RelativeLayout>
</RelativeLayout>
5个回答

2

如果你碰巧正在使用这个:

getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

这可能会引起问题。我最初添加了那行代码来修复状态栏向下推动应用的错误,但它也防止键盘将应用向上推动。

唉。


2

我曾经遇到过同样的问题,不过找到了解决方法。 在我的情况下,我在 Manifeste.xml 中声明了 minSdkVersion=3(Android 1.5),而我的项目是使用 Android 2.2(SDK 8)编译的。理论上应该可以运行,但是实际上却不能! 我将 minsdkVersion 修改为 4(Android 1.6),然后就可以正常运行了。 这只是不同版本的 Android SDK 兼容性问题。 祝你好运。


非常感谢!将minSdkVersion设置为3后,软键盘部分遮挡了TextEdit小部件底部,但将其设置为4后问题得到解决。 - Pieter Witvoet

2

您不应该使用具体的像素值来设置layout_width和layout_height。相反,根据需要使用wrap_parent或fill_parent。

如果您想要像文本视图这样的特定宽度,请使用dpi单位,在不同的屏幕分辨率上正确调整。

关于您特定的虚拟键盘问题,我建议尝试设置ListView以占用剩余空间。

--将除ListView之外的所有内容的高度设置为wrap_parent --将ListView设置为fill_parent,

ListView应占用可用空间的大小,因此当您要求其调整大小时会缩小。


谢谢Mayra的回复,我尝试按照您说的做了。我把所有内容都放在wrap_content里面,除了listview,它是fill_parent。但是当我启动活动时,我只看到顶部的第一个布局和下面填满整个屏幕的listview,但我失去了底部带有EditText的布局,所以我甚至无法点击EditText来显示键盘。 - Luciano
你的布局引用使用了“@+id”。当您指定某些东西在下方、旁边等时,您设置相对位置的视图必须先出现。并且您必须给它一个已经使用“android:layout_toRightOf=@id/txtComment”声明的ID。 - Falmarri
看起来你可以将外层的RelativeLayout改为只是一个orientation="vertical"的LinearLayout。此外,调试布局的一个好工具是HeirarchyViewer,在sdk的tools目录中。它会可视化显示布局的结构。 - Cheryl Simon

1
你可以像这样添加一个滚动视图:

U can add a scrollview like this :

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

..........


</ScrollView>

这会使应用程序专注于编辑文本并调整其在屏幕上的焦点!它也适用于片段内部!

0
如果EditText字段被键盘遮挡,请使用以下代码:
EditText= findViewById(R.id.edittext)
EditText?.getParent()?.requestChildFocus(EditText,EditText)

如果你想让光标在焦点EditText中,那么可以在EditText?.getParent()?.requestChildFocus(EditText,EditText)之后使用EditText.requestFocus()来帮助获取焦点并将光标放在焦点EditText中。这对于编程非常有用。

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