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

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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How can I use CClistview in COCOS2d Android?(如何在 COCOS2d Android 中使用 CClistview?)
cocos2d-android: how to display score(cocos2d-android:如何显示分数)
Converting Flash Animation to Cocos2D(将 Flash 动画转换为 Cocos2D)
Sqlite database not copied from asset folder Android(Sqlite 数据库未从资产文件夹 Android 复制)
SQLite Database Copy Appears Corrupted When Generated by Device and not Emulator(SQLite 数据库副本在由设备而不是模拟器生成时出现损坏)
Android file copy(安卓文件拷贝)