如果键盘打开,则隐藏部分 activity_main.xml (Andro

Hide part of activity_main.xml if keyboard is open (Android)(如果键盘打开,则隐藏部分 activity_main.xml (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

基础教程推荐