软键盘隐藏 EditText

发布于 2024-10-07 13:33:08 字数 3633 浏览 1 评论 0原文

我有一个布局,它使用 EditText 来让用户搜索数据库并填充 ListView。 EditText 距离屏幕顶部大约 2/3(位于 ImageView 上方,后面是一些介绍文本。)

问题是软键盘隐藏了 EditText,因此用户看不到他的内容打字。 (我禁用了自动建议。)

我尝试过 LinearLayout、RelativeLayout、paddings 和不同的对齐/居中,但我仍然无法让它正常工作。 EditText 要么被隐藏,要么被推离屏幕顶部,要么被“压扁”和扭曲。

建议???

一种可能的解决方法是将 EditText 移动到屏幕顶部。然而,这偏离了我得到的图形设计。

另一个可能的解决方法是让软键盘全屏打开(但不知道如何打开)。这仍然会隐藏 EditText,但随后我可以重新启用自动建议,以便用户可以看到他正在输入的内容...有点...因为他只能看到建议的内容他正在打字。

这是我的最新尝试。参见“框架简介”。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="wrap_content" 
 android:layout_height="fill_parent"
 android:orientation="vertical">

 <LinearLayout android:id="@+id/titleContainer"
  android:orientation="horizontal" android:layout_width="fill_parent"
  android:layout_height="wrap_content">
  <TextView android:text="@string/title_string"
   android:textSize="15sp" android:textColor="#FFFFFF"
   android:textStyle="bold" android:paddingLeft="5dp"
   android:layout_height="fill_parent" android:layout_width="wrap_content" />
 </LinearLayout>

 <FrameLayout 
 android:id="@+id/introFrame"
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content"
 android:gravity="center_horizontal" >
 <ImageView 
  android:src="@drawable/main_search_image"
  android:scaleType="center"
  android:layout_gravity="center"
  android:layout_height="wrap_content"
  android:layout_width="wrap_content"/>
 <LinearLayout
  android:orientation="vertical"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center"
  android:paddingTop="140dp" >
  <LinearLayout android:id="@+id/introSearchContainer"
   android:orientation="horizontal" 
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_gravity="center_vertical" >
   <LinearLayout 
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1" >
    <EditText android:id="@+id/intro_search_box" 
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:hint="     Enter keyword     " 
     android:imeOptions="actionGo"   
     android:inputType="textFilter" 
     android:maxLines="1" />
   </LinearLayout>
   <LinearLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    <Button android:id="@+id/intro_search_button" 
     android:background="@drawable/custom_button_go"
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" />
    </LinearLayout>
  </LinearLayout>
  <TextView
   android:text="@string/search_intro"
   android:textSize="15sp"
   android:textColor="#FFFFFF"
   android:layout_height="wrap_content"
   android:layout_width="fill_parent" /> 
 </LinearLayout>
 </FrameLayout>

 <LinearLayout android:id="@+id/listContainer"
  android:orientation="vertical" android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <ListView android:id="@+id/itemlist" android:layout_width="wrap_content"
   android:layout_height="wrap_content" android:cacheColorHint="#00000000" />
  <TextView android:text="No data found" android:layout_width="fill_parent"
   android:layout_height="fill_parent" android:gravity="center"
   android:textSize="16sp" android:textColor="#FFFFFF" android:id="@+id/android:empty" />
 </LinearLayout>

</LinearLayout>

I have a layout that uses an EditText to let users search a database and populate a ListView. The EditText is about 2/3 of the way from the top of the screen (positioned over an ImageView, and followed by some intro text.)

The problem is that the soft keyboard hides the EditText, so the user can't see what he's typing. (I disabled the auto-suggest.)

I've tried LinearLayout, RelativeLayout, paddings, and different alignments/centerings, but I still can't get it to work right. The EditText is either hidden, gets pushed off the top of the screen, or get "squished" and distorted.

Suggestions???

One possible workaround is to move the EditText to the top of the screen. However, this deviates from the graphic design that I was given.

Another possible workaround is for me to make the soft keyboard open in full screen (not sure how, though). This will still hide the EditText, but then I can re-enable the auto-suggestion so the user can see what he's typing... sort of... because he can only see the suggestions for what he's typing.

Here's my latest attempt. See "introFrame".

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="wrap_content" 
 android:layout_height="fill_parent"
 android:orientation="vertical">

 <LinearLayout android:id="@+id/titleContainer"
  android:orientation="horizontal" android:layout_width="fill_parent"
  android:layout_height="wrap_content">
  <TextView android:text="@string/title_string"
   android:textSize="15sp" android:textColor="#FFFFFF"
   android:textStyle="bold" android:paddingLeft="5dp"
   android:layout_height="fill_parent" android:layout_width="wrap_content" />
 </LinearLayout>

 <FrameLayout 
 android:id="@+id/introFrame"
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content"
 android:gravity="center_horizontal" >
 <ImageView 
  android:src="@drawable/main_search_image"
  android:scaleType="center"
  android:layout_gravity="center"
  android:layout_height="wrap_content"
  android:layout_width="wrap_content"/>
 <LinearLayout
  android:orientation="vertical"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center"
  android:paddingTop="140dp" >
  <LinearLayout android:id="@+id/introSearchContainer"
   android:orientation="horizontal" 
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_gravity="center_vertical" >
   <LinearLayout 
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1" >
    <EditText android:id="@+id/intro_search_box" 
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:hint="     Enter keyword     " 
     android:imeOptions="actionGo"   
     android:inputType="textFilter" 
     android:maxLines="1" />
   </LinearLayout>
   <LinearLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    <Button android:id="@+id/intro_search_button" 
     android:background="@drawable/custom_button_go"
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" />
    </LinearLayout>
  </LinearLayout>
  <TextView
   android:text="@string/search_intro"
   android:textSize="15sp"
   android:textColor="#FFFFFF"
   android:layout_height="wrap_content"
   android:layout_width="fill_parent" /> 
 </LinearLayout>
 </FrameLayout>

 <LinearLayout android:id="@+id/listContainer"
  android:orientation="vertical" android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <ListView android:id="@+id/itemlist" android:layout_width="wrap_content"
   android:layout_height="wrap_content" android:cacheColorHint="#00000000" />
  <TextView android:text="No data found" android:layout_width="fill_parent"
   android:layout_height="fill_parent" android:gravity="center"
   android:textSize="16sp" android:textColor="#FFFFFF" android:id="@+id/android:empty" />
 </LinearLayout>

</LinearLayout>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

空气里的味道 2024-10-14 13:33:08

您要寻找的是 Activity 的 windowSoftInputMode 属性。您可以在 AndroidManifest.xml 文件中进行设置,并为其指定一个值,例如:

  • adjustResize:“Activity 的主窗口始终会调整大小,以便为屏幕上的软键盘腾出空间。”
    < br>
  • adjustPan:“活动的主窗口不会调整大小来为软键盘腾出空间。相反,窗口的内容会自动平移,以便当前焦点永远不会被键盘和用户遮挡总是可以看到他们正在输入的内容,这通常不如调整大小那么理想,因为用户可能需要关闭软键盘才能访问窗口的模糊部分并与之交互。”

只要您将布局包装在 ScrollView 中,调整Resize 可能就适合您。如果背景中有位图图像,它可能会产生负面影响,因为它也会调整大小,在这种情况下,您可能需要使用 adjustmentPan。

<activity android:windowSoftInputMode="adjustResize" />

<activity android:windowSoftInputMode="adjustPan" />

更多信息请访问上面的链接。

What you're looking for is the Activity's windowSoftInputMode attribute. You set this in your AndroidManifest.xml file, and give it a value such as:

  • adjustResize: "The activity's main window is always resized to make room for the soft keyboard on screen."
  • adjustPan: "The activity's main window is not resized to make room for the soft keyboard. Rather, the contents of the window are automatically panned so that the current focus is never obscured by the keyboard and users can always see what they are typing. This is generally less desirable than resizing, because the user may need to close the soft keyboard to get at and interact with obscured parts of the window."

adjustResize will probably work for you, as long as you wrap the layout in a ScrollView. It may have negative effects if you have a bitmap image in the background, as it will be resized as well, in which case you may want to use adjustPan instead.

<activity android:windowSoftInputMode="adjustResize" />

or

<activity android:windowSoftInputMode="adjustPan" />

More information is available at the above link.

却一份温柔 2024-10-14 13:33:08

对我有用的方法是将最高级别的 LinearLayout 放入滚动视图中(滚动视图只能有一个子视图)。这允许整个活动/表单向上滚动,并且不会使焦点中的 EditText 混乱。

首先,我设置了我的活动:

<activity android:windowSoftInputMode="adjustPan" />

然后我做了我正在谈论的 ScrollView 事情:

<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
  <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- STUFF -->

  </LinearLayout>
</ScrollView>

What has worked for me is to drop my highest-level LinearLayout in a scrollView (the ScrollView can only have one child). This allowed the entire activity/form to scroll up and not clutter the EditText in focus.

First, I set in my activity:

<activity android:windowSoftInputMode="adjustPan" />

Then I did the ScrollView thing I'm talking about:

<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
  <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- STUFF -->

  </LinearLayout>
</ScrollView>
等风来 2024-10-14 13:33:08

只需在清单中使用以下内容...

  <activity
        android:windowSoftInputMode="adjustPan">

just use following in manifest...

  <activity
        android:windowSoftInputMode="adjustPan">
如何视而不见 2024-10-14 13:33:08

尝试给编辑文本一个layout_weight(即layout_weight = 1),这可能会对您可能需要解决的其他布局项目产生一些其他影响,但这可能有助于它在软键盘弹出时保持可见

try giving the edit text a layout_weight (i.e. layout_weight=1) , this may have some other effects on your other layout items that you may have to work through, but this may help it stay visible when soft keyboard pops up

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文