简体   繁体   English

Android 软键盘覆盖 EditText 字段

[英]Android soft keyboard covers EditText field

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

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.将其添加到包含输入的活动的活动标签中的 manifest.xml。 example:例子:

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

Are you asking how to control what is visible when the soft keyboard opens?您是在问如何控制软键盘打开时可见的内容吗? You might want to play with the windowSoftInputMode .您可能想使用windowSoftInputMode See developer docs for more discussion.有关更多讨论,请参阅开发人员文档

I had the same issue where the softkeyboard was on top of the EditText views which were placed on the bottom of the screen.我遇到了同样的问题,其中软键盘位于位于屏幕底部的 EditText 视图的顶部。 I was able to find a solution by adding a single line to my AndroidManifest.xml file's relevant activity.我能够通过向我的 AndroidManifest.xml 文件的相关活动添加一行来找到解决方案。

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.这里最重要的值是adjustResize。 This will shift the whole UI up to give room for the softkeyboard.这会将整个 UI 向上移动,为软键盘留出空间。

Why not try to add a ScrollView to wrap whatever it is you want to scroll.为什么不尝试添加一个 ScrollView 来包装您想要滚动的任何内容。 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.这是我如何做到的,我实际上在顶部留下了一个不滚动的标题,而当您打开软键盘时,对话框小部件(特别是 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>

I would typically have a LinearLayout inside the ScrollView, but that is up to you.我通常会在 ScrollView 中有一个 LinearLayout,但这取决于您。 Also, setting Scrollbar style to outsideInset helps, at least on my devices.此外,至少在我的设备上,将 Scrollbar 样式设置为 externalInset 有帮助。

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

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

works for android EditText, while it not works for webview or xwalkview.适用于 android EditText,而不适用于 webview 或 xwalkview。 When soft keyboard hide the input in webview or xwalkview you have use android:windowSoftInputMode="adjustResize"当软键盘在 webview 或 xwalkview 中隐藏输入时,您已使用android:windowSoftInputMode="adjustResize"

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.希望有帮助。

将此单行添加到您的相关活动中,其中键盘盖编辑 text.inside onCreat() 活动方法。

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

just add只需添加

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

change paddingBottom according to your size of edittext根据您的编辑文本大小更改 paddingBottom

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解决方案是在我的 styles.xml 中将android:windowTranslucentStatus属性设置为 false

If EditText Field is been covered by the KeyBoard Use the following code:如果键盘覆盖了 EditText 字段,请使用以下代码:

    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.如果您希望光标位于焦点编辑文本中,而不是在EditText?.getParent()?.requestChildFocus(EditText,EditText)之后使用EditText.requestFocus() EditText?.getParent()?.requestChildFocus(EditText,EditText)这有助于在焦点EditText?.getParent()?.requestChildFocus(EditText,EditText)中获得焦点和光标。

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.因此,对于全屏,请在 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);
    }

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);

Edit your AndroidManifest.xml编辑您的 AndroidManifest.xml

android:windowSoftInputMode="adjustResize"

Add this to your root view of Layout file.将此添加到布局文件的根视图中。

android:fitsSystemWindows="true"

That's all.就这样。

I had the same issue and searching the people said to add adjustPan , while in my case adjustResize worked.我遇到了同样的问题并搜索了说要添加adjustPan ,而在我的情况下adjustResize有效。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM