对于EditText,如何让键盘不遮挡它和其他一些视图?

huangapple go评论82阅读模式
英文:

For EditText, how do I let the keyboard not cover it and some other views?

问题

背景

我有一个EditText,当聚焦在它上面时,它应该显示一个键盘,同时显示整个EditText,我可能需要在下面包括一些视图。

问题

由于一些要求,我需要EditText的文本大小较小,但最小高度也要足够。

我已经创建了一个小示例来演示这个问题:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent" android:layout_height="match_parent" android:clipChildren="false"
    android:clipToPadding="false" tools:context=".MainActivity">

    <androidx.appcompat.widget.AppCompatEditText
        android:id="@+id/messageEditText" android:layout_width="0px" android:layout_height="wrap_content"
        android:hint="在这里输入" android:minHeight="?attr/actionBarSize" android:textSize="12sp"
        app:layout_constrainedWidth="true" app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" tools:background="" />

</androidx.constraintlayout.widget.ConstraintLayout>

当我聚焦在EditText上时,出现了一些问题,我无法看到它的下划线:

对于EditText,如何让键盘不遮挡它和其他一些视图?

在清单中,我设置了android:windowSoftInputMode="adjustPan",因为要求是移动元素而不是调整它们的大小。

我的尝试

我尝试添加内边距,还尝试添加一个虚拟的底部可绘制对象。这些只能添加空白空间,但会使下划线离可见区域更远。

问题

是否可能使EditText具有这种最小高度和文本大小,同时在键盘出现时显示下划线?

是否还可以在EditText下方显示更多内容?将EditText放得太靠近键盘不太好看。我知道在这个示例中我只有EditText,但想象一下,如果下面还有更多视图会怎样。

英文:

Background

I have an EditText, that when focusing on it, it should show a keyboard and yet show the entire EditText and I might need to include few Views below.

The problem

Due to some requirements, I need the EditText to be of some small text size yet have a minimal height too.

I've made a snippet to make a tiny example of the issue:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot; xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot; android:layout_height=&quot;match_parent&quot; android:clipChildren=&quot;false&quot;
    android:clipToPadding=&quot;false&quot; tools:context=&quot;.MainActivity&quot;&gt;

    &lt;androidx.appcompat.widget.AppCompatEditText
        android:id=&quot;@+id/messageEditText&quot; android:layout_width=&quot;0px&quot; android:layout_height=&quot;wrap_content&quot;
        android:hint=&quot;write here&quot; android:minHeight=&quot;?attr/actionBarSize&quot; android:textSize=&quot;12sp&quot;
        app:layout_constrainedWidth=&quot;true&quot; app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot; app:layout_constraintStart_toStartOf=&quot;parent&quot; tools:background=&quot;&quot; /&gt;

&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

When I focus on the EditText, for some reason I don't see its line:

对于EditText,如何让键盘不遮挡它和其他一些视图?

In the manifest, I have android:windowSoftInputMode=&quot;adjustPan&quot; because the requirement is to move things and not resize them.

What I've tried

I tried to add padding, and I tried to add a fake bottom drawable. Those can only add empty space, but will make the line even further from being shown.

The question

Is it possible to have the EditText have this minimal height and textsize, while also letting it line being shown when the keyboard appears?

Is it also possible to even show a bit more below the EditText ? It's not so nice to have the EditText be so near the keyboard. I know that in this snippet I only have the EditText, but imagine I would have more views below it.

答案1

得分: 1

没有办法。当键盘打开时,只有3种反应方式:

  1. 什么都不做
  2. 调整大小
  3. 平移,以使光标所在行在屏幕上可见。

没有其他可做的方式,也没有可靠的信号发送给应用程序表明键盘正在显示,你可以对此作出响应。尤其是无法确保EditText底部的行将显示在屏幕上。

你所能做的最好的办法是调整大小,并仔细布局内容,以使调整对显示的用户界面产生最小影响。但这可能取决于界面的具体情况,而你还没有向我们展示足够的信息,以便我们为你提供建议。

英文:

There is no way. THere are only 3 ways of reacting when the keyboard opens:

1)Do nothing
2)Resize
3)Pan, such that the line with the cursor is on screen.

There is no way to do anything else, and no reliable signal sent to the app that the keyboard is showing you can respond to. There especially is no way to ensure the line at the bottom of an EditText is going to be on screen.

The best you can do to try to ensure that is to resize, and carefully layout your content so that the resize has minimal effect on the ui shown. But that may or may not be possible, depending on the specifics of the UI. And you haven't shown us enough of what you're trying to do to give you suggestions on it.

答案2

得分: 0

在AndroidManifest.xml文件中使用以下代码:

<activity
    android:name="com.example.MainActivity"
    android:label="@string/activity_main"
    android:windowSoftInputMode="adjustResize|stateHidden" />

这段代码对我有效。在清单文件中的活动标签中添加这个属性:

android:windowSoftInputMode="adjustResize|stateHidden"
英文:

Use the code in AndroidManifest.xml file:

&lt;activity
        android:name=&quot;com.example.MainActivity&quot;
        android:label=&quot;@string/activity_main&quot;
        android:windowSoftInputMode=&quot;adjustResize|stateHidden&quot; /&gt;

This code worked for me.
Inside the manifest file in activity tag add this attribute:

> android:windowSoftInputMode="adjustResize|stateHidden"

答案3

得分: 0

你可以在 Android 清单文件中设置 windowSoftInputMode

在你的情况下,我认为你应该使用 adjustPan。你的窗口大小不会改变,但内容会向上滚动。以下是关于 adjustPan 的文档说明:

活动的主窗口不会调整大小以腾出空间容纳软键盘。相反,窗口的内容会自动平移,以确保当前焦点不会被键盘遮挡,用户始终可以看到他们正在输入的内容。一般情况下,这比调整大小不太理想,因为用户可能需要关闭软键盘才能访问和与窗口中被遮挡的部分交互。

如果你不想让内容视图布局平移,从而使内容滚出屏幕,你可以使用 adjustResize

如果你想了解更多信息,你可以阅读官方文档

英文:

you can in the android manifest set windowSoftInputMode .

&lt;activity
android:windowSoftInputMode=[&quot;stateUnspecified&quot;,
                                       &quot;stateUnchanged&quot;, &quot;stateHidden&quot;,
                                       &quot;stateAlwaysHidden&quot;, &quot;stateVisible&quot;,
                                       &quot;stateAlwaysVisible&quot;, &quot;adjustUnspecified&quot;,
                                       &quot;adjustResize&quot;, &quot;adjustPan&quot;] &gt; 
&lt;/activity&gt;

In your condition, I think you should use adjustPan. your window size it not change, but the content translates up. the following is the document says about 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.

if you don't want the content view layout translate, which makes the content scroll out screen. you can use adjustResize

if you want to know more, you can read the official document

huangapple
  • 本文由 发表于 2020年1月6日 19:21:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/59611206.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定