隐藏activity_main.xml中的一部分,如果键盘打开(Android版) [英] Hide part of activity_main.xml if keyboard is open (Android)
问题描述
我有以下的 activity_main.xml中是在底部。在那里,一个 CoordinatorLayout
和两个 LinearLayouts
出现。第一LinearLayout1有一个的EditText
输入一些文字,一个按钮发送。第二个还有其他两个按钮。
如果我点击进入的EditText
,键盘显示出来,使一切都在LinearLayout2隐约可见。所以,我想隐藏在最后一个布局这两个按钮时,键盘打开。
我已经找到了使用的android:windowSoftInputMode =stateHidden
可能的伎俩,但仅在 AndroidManifest.xml中与活动有关
。我只是想用这种内在的第二线性布局。已经尝试到那里使用它,但没有成功。
另外,我做什么,在我的XML处理这一点,让我的MainActivity- code干净。这方面的任何可能性?
<?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android>
...
< android.support.design.widget.CoordinatorLayout>
< android.support.v7.widget.RecyclerView />
< /android.support.design.widget.CoordinatorLayout> <&的LinearLayout GT;
<的EditText />
<按钮机器人:ID =@ + ID / button_send/>
< / LinearLayout中> <&的LinearLayout GT;
<按钮/>
<按钮/>
< / LinearLayout中>< / LinearLayout中>
我们已经在过去同样的问题,所以我们已经把一些可观察到的全布局对象
试试这个,让我知道
/ *隐藏按钮,当键盘打开* /
view.getViewTreeObserver()。addOnGlobalLayoutListener(新ViewTreeObserver.OnGlobalLayoutListener(){
@覆盖
公共无效onGlobalLayout(){
矩形R =新的矩形();
view.getWindowVisibleDisplayFrame(R); INT heightDiff = view.getRootView()的getHeight() - (r.bottom - r.top)。 如果(heightDiff> 244){//如果超过100像素,它可能是一个键盘...
// OK,现在我们知道了键盘起来......
buttonLayout.setVisibility(View.GONE);
}其他{
// OK,现在我们知道键盘是下来...
buttonLayout.setVisibility(View.VISIBLE);
}
}
});
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.
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.
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.
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>
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版)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!