Push up content except some view when keyboard shown(显示键盘时向上推送内容,但某些视图除外)
问题描述
我的布局在底部包含 5 个 EditText 和一个
Button
和一个 TextView
.现在,当我按下 EditText
时,键盘将显示并且 all 我的 View
向上推.
I have a layout that contains 5 EditText
and a Button
and a TextView
at bottom. Now when I press an EditText
then the keyboard will shown and all my View
is push up.
现在我不想将我的 TextView
和 Button
推到键盘上方,只想全部推上 ScrollView
内的 EditText 到键盘上方.
Now I don't want to push my TextView
and Button
to above keyboard, just only want to push up all EditText
inside ScrollView
to above keyboard.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ff0"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<EditText
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="EditText 1"
/>
<EditText
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="EditText 2"
/>
<EditText
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="EditText 3"
/>
<EditText
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="EditText 4"
/>
<EditText
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="EditText 5"
android:inputType="textNoSuggestions"
/>
</LinearLayout>
</ScrollView>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Button"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:text="I don't want to push this TextView and Button to above keyboard when keyboard is shown. Just obly want to push the ScrollView that contain all EditText"
/>
</LinearLayout>
我有个想法是.当键盘显示和隐藏时我会监听.当键盘显示时,我将设置 ScrollView 的底部边距 = 键盘高度,当键盘隐藏时,我将设置此边距 = 0.
有什么方法更容易处理我的案子吗?任何帮助或建议将不胜感激.
I have an idea is. When I will listener when keyboard show and hide. When keyboard show I will set the bottom margin of ScrollView = keyboard height, when keyboard hide I will set this margin = 0.
Is there any way easier to handle my case? Any help or suggestion would be great appreciated.
更新
如果我使用 windowSoftInputMode=adjustPan
=> 并非所有 EditText
都被推到键盘上方
如果我使用 windowSoftInputMode=adjustResize
=> Button
, TextView
并且所有 EditText
都被推到键盘上方
UPDATE
If I use windowSoftInputMode=adjustPan
=> not all EditText
is push up to above keyboard
If I use windowSoftInputMode=adjustResize
=> Button
, TextView
and all EditText
is push up to above keyboard
推荐答案
有了 WindowInsetsCompat
的新功能,可以帮助轻松检测键盘可见性(我在这里按照答案 https://stackoverflow.com/a/63517673/5381331).
而不是像以前那样使用 windowSoftInputMode=adjustPan
来降低复杂性,现在我可以通过将 windowSoftInputMode=adjustResize
与 WindowInsetsCompat
With the new feature of WindowInsetsCompat
which help detecting keyboard visibility easily (I follow answer here https://stackoverflow.com/a/63517673/5381331).
Instead of using windowSoftInputMode=adjustPan
for less complex like before, now I can solve this problem by combine windowSoftInputMode=adjustResize
with WindowInsetsCompat
ViewCompat.setOnApplyWindowInsetsListener(root) { v, insets ->
val isKeyboardVisible = insets.isVisible(WindowInsetsCompat.Type.ime())
if (isKeyboardVisible) {
// Hide some view when keyboard visible
} else {
// Show it again here
}
}
AndroidManifest
AndroidManifest
<activity android:name=".activity_name"
android:windowSoftInputMode="adjustResize"> // all content will push to above keyboard then we able to scroll to see whole content
</activity>
这篇关于显示键盘时向上推送内容,但某些视图除外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:显示键盘时向上推送内容,但某些视图除外
基础教程推荐
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01