Hide part of activity_main.xml if keyboard is open (Android)(如果键盘打开,则隐藏部分 activity_main.xml (Android))
问题描述
我在底部显示了以下 activity_main.xml.在那里,出现了一个 CoordinatorLayout
和两个 LinearLayouts
.第一个 LinearLayout1 有一个 EditText
来输入一些文本,并带有一个按钮来发送它.第二个有另外两个按钮.
I have the following activity_main.xml shown at bottom. There, a CoordinatorLayout
and two LinearLayouts
occur. The first LinearLayout1 has an EditText
to input some text, with a button to send it. The second one has two other buttons.
如果我点击 EditText
,键盘会出现,并使 LinearLayout2 中的所有内容几乎不可见.所以,我想在键盘打开时隐藏最后一个布局中的两个按钮.
If i click into the EditText
, the keyboard shows up and makes everything in LinearLayout2 barely visible. So, i want to hide both buttons in last layout when keyboard is open.
我已经发现使用 android:windowSoftInputMode="stateHidden"
可能是诀窍,但仅与 AndroidManifest.xml
中的活动有关.我只想在第二个线性布局中使用这个内部.已经尝试在那里使用它,但没有成功.
I already found out to use android:windowSoftInputMode="stateHidden"
may be the trick, but only in connection with activities in AndroidManifest.xml
. I just want to use this inner the second linear layout. Already tried to use it there, but with no success.
此外,我要在我的 xml 中处理这个问题并保持我的 MainActivity 代码干净.这有什么可能吗?
In addition, i what to handle this in my xml and keep my MainActivity-code clean. Any possibilities for this?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
...
<android.support.design.widget.CoordinatorLayout>
<android.support.v7.widget.RecyclerView/>
</android.support.design.widget.CoordinatorLayout>
<LinearLayout>
<EditText/>
<Button android:id="@+id/button_send"/>
</LinearLayout>
<LinearLayout>
<Button/>
<Button/>
</LinearLayout>
</LinearLayout>
推荐答案
我们过去也遇到过同样的问题,所以我们在完整布局对象上放置了一些 observable试试这个,让我知道
We had same issue in past so we have put some observable on full layout object try this and let me know
/*Hide button when keyboard is open*/
view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect r = new Rect();
view.getWindowVisibleDisplayFrame(r);
int heightDiff = view.getRootView().getHeight() - (r.bottom - r.top);
if (heightDiff > 244) { // if more than 100 pixels, its probably a keyboard...
//ok now we know the keyboard is up...
buttonLayout.setVisibility(View.GONE);
} else {
//ok now we know the keyboard is down...
buttonLayout.setVisibility(View.VISIBLE);
}
}
});
这篇关于如果键盘打开,则隐藏部分 activity_main.xml (Android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如果键盘打开,则隐藏部分 activity_main.xml (Andro
基础教程推荐
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01