Android 软键盘覆盖 EditText 字段

发布于 2024-09-10 09:27:18 字数 27 浏览 5 评论 0原文

有没有办法让屏幕滚动以允许看到文本字段?

Is there a way to make the screen scroll to allow the text field to be seen?

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

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

发布评论

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

评论(16

相思碎 2024-09-17 09:27:18

我有同样的问题。尝试以下代码:

android:windowSoftInputMode="adjustPan"

将其添加到保存输入的活动的活动标记中的manifest.xml中。例子:

<activity
            android:name=".Activities.InputsActivity"
            ...
            android:windowSoftInputMode="adjustPan"
            />

I had same issues. Try following code:

android:windowSoftInputMode="adjustPan"

add it to your manifest.xml in the activity tag of the activity that holds the input. example:

<activity
            android:name=".Activities.InputsActivity"
            ...
            android:windowSoftInputMode="adjustPan"
            />
久伴你 2024-09-17 09:27:18

您是想问如何控制软键盘打开时可见的内容吗?您可能想使用 windowSoftInputMode。有关详细讨论,请参阅开发者文档

Are you asking how to control what is visible when the soft keyboard opens? You might want to play with the windowSoftInputMode. See developer docs for more discussion.

半枫 2024-09-17 09:27:18

我遇到了同样的问题,软键盘位于位于屏幕底部的 EditText 视图的顶部。我能够通过向 AndroidManifest.xml 文件的相关活动添加一行来找到解决方案。

android:windowSoftInputMode="adjustResize|stateHidden"

整个活动标签如下所示:

<activity
        android:name="com.my.MainActivity"
        android:screenOrientation="portrait"
        android:label="@string/title_activity_main"
        android:windowSoftInputMode="adjustResize|stateHidden" >
    </activity>

这里最重要的值是 adjustmentResize。这会将整个 UI 向上移动,为软键盘腾出空间。

I had the same issue where the softkeyboard was on top of the EditText views which were placed on the bottom of the screen. I was able to find a solution by adding a single line to my AndroidManifest.xml file's relevant activity.

android:windowSoftInputMode="adjustResize|stateHidden"

This is how the whole activity tag looks like:

<activity
        android:name="com.my.MainActivity"
        android:screenOrientation="portrait"
        android:label="@string/title_activity_main"
        android:windowSoftInputMode="adjustResize|stateHidden" >
    </activity>

Here the most important value is the adjustResize. This will shift the whole UI up to give room for the softkeyboard.

琉璃梦幻 2024-09-17 09:27:18

为什么不尝试添加一个 ScrollView 来包装您想要滚动的任何内容。这是我的做法,我实际上在顶部留下了一个不滚动的标题,而当您打开软键盘时对话框小部件(特别是 EditTexts)会滚动。

<LinearLayout android:id="@+id/HeaderLayout" >
  <!-- Here add a header or whatever will not be scrolled. -->
</LinearLayout>
<ScrollView android:id="@+id/MainForm" >
  <!-- Here add your edittexts or whatever will scroll. -->
</ScrollView>

我通常会在 ScrollView 中使用 LinearLayout,但这取决于你。另外,将滚动条样式设置为 OutsideInset 会有所帮助,至少在我的设备上是这样。

Why not try to add a ScrollView to wrap whatever it is you want to scroll. Here is how I have done it, where I actually leave a header on top which does not scroll, while the dialog widgets (in particular the EditTexts) scroll when you open soft keypad.

<LinearLayout android:id="@+id/HeaderLayout" >
  <!-- Here add a header or whatever will not be scrolled. -->
</LinearLayout>
<ScrollView android:id="@+id/MainForm" >
  <!-- Here add your edittexts or whatever will scroll. -->
</ScrollView>

I would typically have a LinearLayout inside the ScrollView, but that is up to you. Also, setting Scrollbar style to outsideInset helps, at least on my devices.

无风消散 2024-09-17 09:27:18

您需要做的就是

android:isScrollContainer="true"

来源: http: //www.davidwparker.com/2011/08/25/android-fixing-window-resize-and-scrolling/

All you need to do is

android:isScrollContainer="true"

source: http://www.davidwparker.com/2011/08/25/android-fixing-window-resize-and-scrolling/

病女 2024-09-17 09:27:18

很抱歉恢复旧线程,但没有人提到在您的 EditText 元素中设置 android:imeOptions="flagNoFullscreen"

Sorry for reviving an old thread but no one mentioned setting android:imeOptions="flagNoFullscreen" in your EditText element

眼睛会笑 2024-09-17 09:27:18
android:windowSoftInputMode="adjustPan"
android:isScrollContainer="true"

适用于 android EditText,但不适用于 webview 或 xwalkview。当软键盘隐藏 webview 或 xwalkview 中的输入时,您必须使用 android:windowSoftInputMode="adjustResize"

android:windowSoftInputMode="adjustPan"
android:isScrollContainer="true"

works for android EditText, while it not works for webview or xwalkview. When soft keyboard hide the input in webview or xwalkview you have use android:windowSoftInputMode="adjustResize"

傻比既视感 2024-09-17 09:27:18

我相信您可以使用轨迹球使其滚动,这最终可能通过选择方法以编程方式实现,但这只是一个想法。我知道轨迹球方法通常有效,但至于执行此操作并使其通过代码工作的确切方法,我不确定。
希望有帮助。

I believe that you can make it scroll by using the trackball, which might be achieved programmatically through selection methods eventually, but it's just an idea. I know that the trackball method typically works, but as for the exact way to do this and make it work from code, I do not sure.
Hope that helps.

〆凄凉。 2024-09-17 09:27:18

将这一行添加到您的相关活动中,其中键盘覆盖编辑文本。活动的 onCreat() 方法内。

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

add this single line to your relative activity where key board cover edit text.inside onCreat()method of activity.

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
弃爱 2024-09-17 09:27:18

只需添加

android:gravity="bottom" android:paddingBottom="10dp"

根据您的编辑文本大小更改 paddingBottom

just add

android:gravity="bottom" android:paddingBottom="10dp"

change paddingBottom according to your size of edittext

吲‖鸣 2024-09-17 09:27:18

我的解决方案是将布局放在 ScrollView 中,如果内容超过屏幕高度,用户可以滚动查看整个内容,并且内容的 EditText 将完全可见。如果它位于内容的末尾,则需要为要显示的视图添加足够的底部边距。

My solution is to put the layout in a ScrollView, If the content is over a screen height, user can scroll to see the whole content and the EditText of the content will fully be seen. If it is at the end of the content, to add enough margin bottom for the view to be shown is necessary.

新一帅帅 2024-09-17 09:27:18

我知道这已经过时了,但上述解决方案都不适合我。经过大量调试后,我发现了这个问题。
解决方案是在 styles.xml 中将 android:windowTranslucentStatus 属性设置为 false

I know this is old but none of the above solutions worked for me. After extensive debugging, I figured out the issue.
The solution is setting the android:windowTranslucentStatus attribute to false in my styles.xml

恋竹姑娘 2024-09-17 09:27:18

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

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

如果您希望光标位于聚焦的 EditText 中,则在 之后使用 EditText.requestFocus() >EditText?.getParent()?.requestChildFocus(EditText,EditText) 这有助于在聚焦的 EditText 中获取焦点和光标。

If EditText Field is been covered by the KeyBoard Use the following code:

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

If you want the Cursor to be in the Focused EditText than use EditText.requestFocus() after the EditText?.getParent()?.requestChildFocus(EditText,EditText) which helps to get the focus and Cursor in the Focused EditText.

北城挽邺 2024-09-17 09:27:18

上述解决方案工作完美,但如果您使用全屏活动(半透明状态栏),那么这些解决方案可能不起作用。
因此,要全屏显示,请在 Activity 的 onCreate 函数中使用此代码。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        Window w = getWindow();
        w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN , WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN );
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.TYPE_STATUS_BAR);
    }

例子:

  protected void onCreate(Bundle savedInstanceState) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        Window w = getWindow();
        w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN , WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN );
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.TYPE_STATUS_BAR);
    }
    
    super.onCreate(savedInstanceState);

The above solution work perfectly but if you are using Fullscreen activity(translucent status bar) then these solutions might not work.
so for fullscreen use this code inside your onCreate function on your Activity.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        Window w = getWindow();
        w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN , WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN );
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.TYPE_STATUS_BAR);
    }

Example:

  protected void onCreate(Bundle savedInstanceState) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        Window w = getWindow();
        w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN , WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN );
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.TYPE_STATUS_BAR);
    }
    
    super.onCreate(savedInstanceState);
天冷不及心凉 2024-09-17 09:27:18

编辑 AndroidManifest.xml

android:windowSoftInputMode="adjustResize"

将其添加到布局文件的根视图中。

android:fitsSystemWindows="true"

就这样。

Edit your AndroidManifest.xml

android:windowSoftInputMode="adjustResize"

Add this to your root view of Layout file.

android:fitsSystemWindows="true"

That's all.

爱的十字路口 2024-09-17 09:27:18

我遇到了同样的问题,并搜索了说要添加 adjustmentPan 的人,而在我的情况下, adjustmentResize 有效。

<activity
    android:name=".YOUR.ACTIVITY"
    ...
    android:windowSoftInputMode="adjustResize"
/>

I had the same issue and searching the people said to add adjustPan, while in my case adjustResize worked.

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