【问题标题】:How to prevent that the keyboard hides my EditText?如何防止键盘隐藏我的 EditText?
【发布时间】:2011-03-29 13:27:53
【问题描述】:

我在顶部有一个 RelativeLayout,然后在下面我在中心有一个 ListView,最后在底部我有另一个 relativeLayout,里面有一个 EditText 和一个 Button。

当我单击 EditText 并出现 IME(虚拟键盘)时,我希望 ListView 调整大小。如果我将 adjustResize 放入清单中,则 Listview 的大小将调整以为 IME 留出空间,但带有下方 EditText 的 RelativeLayout 被 IME 覆盖,我看不到我在写什么。如果我放了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>

【问题讨论】:

    标签: android keyboard android-edittext


    【解决方案1】:

    你不应该用明确的像素值来设置你的 layout_width 和 layout_heights。相反,请酌情使用 wrap_parent 或 fill_parent。

    如果您希望像文本视图这样的东西具有特定的宽度,请使用可以在不同屏幕分辨率上正确调整的 dpi 单位。

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

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

    ListView 应该占用多少可用空间,因此当您要求它调整大小时会缩小。

    【讨论】:

    • 感谢 Mayra 的回复,我试着照你说的做。我把所有的东西都放在了 wrap_content 中,除了我放在 fill_parent 中的列表视图。但是当我开始活动时,我只看到顶部的第一个布局和下面的列表视图,它填充了屏幕的所有其余部分,但是我失去了带有 EditText 的底部布局,所以我什至无法单击 edittext 来显示键盘
    • 您的布局引用正在使用“@+id”。当您指定某物在其他物的下方、旁边等时,您设置它的相对视图必须首先出现。你必须给它一个已经使用 `android:layout_toRightOf="@id/txtComment" 声明的 ID
    • 看起来您应该能够将外部 RelativeLayout 更改为仅具有方向 =“vertical”的 LinearLayout。此外,调试布局的好工具是 sdk 的工具目录中的 HeirarchyViewer。它将可视化它认为布局的方式。
    【解决方案2】:

    我遇到了同样的问题,我找到了解决方案。 就我而言,在 Manifeste.xml 中,我声明了 minSdkVersion=3 (Android 1.5),并且我的项目是使用 Android 2.2 (SDK 8) 编译的。理论上,它应该可以工作......但不是! 我把minsdkVersion修改为4(Android 1.6),就OK了。 这只是不同 Android SDK 之间的兼容性问题。 祝你好运。

    【讨论】:

    • 非常感谢!将 minSdkVersion 设置为 3 时,我的软键盘部分遮挡了 TextEdit 小部件的底部,但将其设置为 4 可以解决此问题。
    【解决方案3】:

    如果你有任何机会使用这个:

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

    这可能会导致问题。我最初添加该行是为了修复状态栏向下推动应用程序的错误......但它也阻止了键盘向上推动应用程序。

    叹息。

    【讨论】:

      【解决方案4】:

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

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

      这将导致应用专注于编辑文本并将其调整为专注于屏幕!它也适用于片段!

      【讨论】:

        【解决方案5】:

        如果 EditText 字段被键盘覆盖,请使用以下代码:

        EditText= findViewById(R.id.edittext)
        EditText?.getParent()?.requestChildFocus(EditText,EditText)
        

        如果您希望 Cursor 位于 Focused EditText 中而不是使用 EditText.requestFocus() after EditText?.getParent()?.requestChildFocus(EditText,EditText) 这有助于在 Focused EditText 中获得焦点和光标.

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-08-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-09-07
          • 2016-02-18
          • 2022-09-29
          相关资源
          最近更新 更多