Android 单击编辑文本时上推内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11292950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:46:41  来源:igfitidea点击:

Push up content when clicking in edit text

android

提问by Neil

I have searched everywhere for a solution to my problem but cannot find a answer. Here's the problem.

我到处搜索我的问题的解决方案,但找不到答案。问题就在这里。

I have a layout that looks like this

我有一个看起来像这样的布局

Image is here

图片在这里

Now when I click in the edit text(search bar) i want the following to happen

现在,当我单击编辑文本(搜索栏)时,我希望发生以下情况

Image is here

图片在这里

The soft keyboard basically needs to push up the whole screens content so that the search bar is at the top and its listview is beneath it so that when the content is searched the results are displayed. I have tried setting android:windowSoftInputMode="adjustPan"to the Activityin the manifest but this did not work. I set a scroll view as the main container in the main layout that contains the fragments but that also did not work. I have tried adding the edit text(search bar) as a header to the list view but this also did not work. Every time the keyboard pushed up the edit text but covered the list view. Is there any way to make this work?

软键盘基本上需要将整个屏幕内容上推,使搜索栏在顶部,列表视图在其下方,以便在搜索内容时显示结果。我曾尝试在清单中设置android:windowSoftInputMode="adjustPan"为 ,Activity但这不起作用。我将滚动视图设置为包含片段但也不起作用的主布局中的主容器。我尝试将编辑文本(搜索栏)作为标题添加到列表视图中,但这也不起作用。每次键盘向上推编辑文本但覆盖列表视图。有什么办法可以使这项工作?

回答by David Fischer

Actually if you want your entire layout pan up than you should use :

实际上,如果您想要整个布局平移而不是应该使用:

SOFT_INPUT_ADJUST_PAN

SOFT_INPUT_ADJUST_PAN

meaning:

意义:

getActivity().getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

this will keep the keyboard closed and when opened it'll push your entire activity up.

这将使键盘保持关闭状态,打开时它将推动您的整个活动。

回答by Dunken_sai

This can be used in Fragment Class to move the Edit text up and Scroll till end.

这可以在 Fragment Class 中使用以将 Edit 文本向上移动并滚动到最后。

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

This will work for sure if your Fragment Activity theme is not in FullScreen.

如果您的 Fragment Activity 主题不在全屏模式下,这肯定会起作用。

Hope this will help!

希望这会有所帮助!

回答by Madhav

This worked for me.

这对我有用。

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

回答by Neo

Try this

尝试这个

android:windowSoftInputMode="adjustResize"

Working for me.

为我工作。

回答by Chandru

I faced a similar issue, the solution was simple.

我遇到了类似的问题,解决方案很简单。

If the theme in the activity is set as Fullscreen @android:style/Theme.Holo.NoActionBar.FullScreen, the keyboard was not pushing the contents up.

如果活动中的主题设置为 Fullscreen @android:style/Theme.Holo.NoActionBar.FullScreen,则键盘不会向上推内容。

After I changed the activity to @android:style/Theme.Holo.NoActionBar, the keyboard is now pushing the activity contents up.

在我将活动更改为 后@android:style/Theme.Holo.NoActionBar,键盘现在将活动内容向上推。

回答by Barak

Try android:windowSoftInputMode="adjustResize"in your manifest file.

android:windowSoftInputMode="adjustResize"在您的清单文件中尝试。

You might need to do android:windowSoftInputMode="adjustResize|adjustPan"... I don't really have anything comparable to your setup to test on...

你可能需要做android:windowSoftInputMode="adjustResize|adjustPan"......我真的没有任何可以与你的设置相媲美的东西来测试......

Your problem is becasue of this (from the docs for adjustPan, emphasis mine):

你的问题是因为这个(来自于 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 focusis never obscured by the keyboard and users can always see what they are typing

活动的主窗口没有调整大小来为软键盘腾出空间。相反,窗口的内容会自动平移,因此当前焦点永远不会被键盘遮挡,用户始终可以看到他们正在输入的内容

Given that, your list is being obscured becasue the EditText on top has the focus. The only thing I can currently think of is to move the EditText so it is below the list. Then when pushed up, your list should still be visible above the EditText.

鉴于此,您的列表被遮挡,因为顶部的 EditText 具有焦点。我目前唯一能想到的是将 EditText 移动到列表下方。然后,当向上推时,您的列表仍应在 EditText 上方可见。

回答by Mihaela Tudurache

This worked for me, for fragment, just declare it in Manifest with activity:

这对我有用,对于片段,只需在清单中声明它的活动:

android:windowSoftInputMode="adjustPan"

Good Luck!

祝你好运!

回答by Neil

I could not find a solution for the layout to change automatically so I had to hack it. I used the code that I found herein my main activity to detect when the keyboard is opened. I just added code to call a method that hides my top two fragments when the keyboard is opened. This isn't really what I wanted to do but it works.

我找不到布局自动更改的解决方案,所以我不得不破解它。我使用的是发现的代码在这里我主要活动的键盘打开时检测。我刚刚添加了代码来调用一个方法,该方法在键盘打开时隐藏我的前两个片段。这不是我真正想做的,但它确实有效。

回答by Petro

I use this for my EditTextlogin:

我用它来EditText登录:

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN|WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

Some notes about this method:

关于此方法的一些注意事项:

thisis referring to the context, as getActivity()is not always preferable

this指的是上下文,这getActivity()并不总是可取的

.SOFT_INPUT_STATE_HIDDEN  //for default hidden state when activity loads

.SOFT_INPUT_STATE_VISIBLE //for auto open keyboard when activity loads 

Works everytime!

每次都有效!

回答by Manish Malviya

If anyone is still facing this issue:

如果有人仍然面临这个问题:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:id="@+id/view"
    android:fitsSystemWindows="true"
    android:layout_height="match_parent">

add in your layout:

在您的布局中添加:

android:fitsSystemWindows="true"

I hope this solves your problem - it worked for me.

我希望这能解决您的问题 - 它对我有用。