【问题标题】:How to prevent keyboard from pushing some elements above the keyboard in android studio?如何防止键盘在android studio中将某些元素推到键盘上方?
【发布时间】:2017-06-06 05:26:27
【问题描述】:

每当激活键盘时,OPEN、SAVE、CLEAR 三个按钮(如上图所示)就会上升。有什么办法不让他们上去吗?

这是我的activity_main.xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:fitsSystemWindows="true"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="p32929.cgpacalculator.Activities.MainActivity">

    <TextView
        android:layout_width="match_parent"

        android:layout_height="wrap_content"
        android:text="@string/input"
        android:textAlignment="center"
        android:textSize="15sp"
        android:textStyle="normal|bold" />


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="100">

        <EditText
            android:id="@+id/mainCredit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="50"
            android:hint="Credit"
            android:inputType="numberDecimal"
            android:maxLines="1"
            android:nextFocusDown="@+id/mainGpa"
            android:textAlignment="center"
            android:textColor="#000000"
            android:textColorHint="@color/hint" />

        <EditText
            android:id="@+id/mainGpa"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="50"
            android:hint="GPA"
            android:imeOptions="actionDone"
            android:inputType="numberDecimal"
            android:textAlignment="center"
            android:textColor="#000000"
            android:textColorHint="@color/hint" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingBottom="7dp"
        android:weightSum="10">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="3"
            android:gravity="center"
            android:text="CGPA = "
            android:textAlignment="center"
            android:textColor="#000000"
            android:textSize="24sp" />

        <TextView
            android:id="@+id/mainCGPA"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="4"
            android:gravity="center"
            android:text="0.00"
            android:textAlignment="center"
            android:textColor="#000000"
            android:textSize="24sp" />

        <Button
            android:id="@+id/MainAdd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:gravity="center"
            android:onClick="Clicked"
            android:text="Add" />

    </LinearLayout>

    <TextView
        android:id="@+id/infotext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:textColor="#5c5c5c"
        android:textStyle="normal|bold" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="3dp"
        android:layout_marginLeft="3dp"
        android:layout_marginRight="3dp"
        android:layout_marginTop="3dp"
        android:weightSum="100">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="25"
            android:text="Serial"
            android:textAlignment="center"
            android:textColor="@color/text"
            android:textSize="14sp"
            android:textStyle="normal|bold" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:layout_weight="25"
            android:text="Credit(s)"
            android:textAlignment="center"
            android:textColor="@color/text"
            android:textStyle="normal|bold" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="13dp"
            android:layout_weight="25"
            android:text="GPA"
            android:textAlignment="center"
            android:textColor="@color/text"
            android:textStyle="normal|bold" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:layout_weight="25"
            android:text="Total"
            android:textAlignment="center"
            android:textColor="@color/text"
            android:textStyle="normal|bold" />
    </LinearLayout>

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="388dp"
        android:layout_weight="3.58"
        android:choiceMode="multipleChoice"
        android:paddingTop="3dp">


    </ListView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="9">

        <Button
            android:id="@+id/openData"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:onClick="openData"
            android:text="Open" />

        <Button
            android:id="@+id/saveData"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:onClick="saveData"
            android:text="Save" />

        <Button
            android:id="@+id/clearData"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:onClick="clearData"
            android:text="Clear" />

    </LinearLayout>


</LinearLayout>

我已经尝试了这里给出的所有解决方案:Android: How do I prevent the soft keyboard from pushing my view up?

但还是一样。

【问题讨论】:

    标签: android xml layout view android-softkeyboard


    【解决方案1】:

    从您的 xml 中删除 android:fitsSystemWindows="true" 或将其更改为 'false'

    【讨论】:

    • @p32929 来自布局 xml 父元素。对你来说,它是LinerarLayoutandroid:id="@+id/activity_main"
    【解决方案2】:

    这个问题被问了很多,答案也很多,但是..

    没有可靠的 API 来检测键盘何时显示。您会在此站点上看到“解决方案”,但它们有误报和误报。但似乎对您来说最好的事情是将softInputMode 设置为adjustPan。这将使操作系统以使光标在键盘上方可见所需的最小量滚动整个屏幕。 (整个应用程序在键盘上方是由于模式adjustResize)。

    引用@Gabe Sechan 来自latest bounty question 的相关主题


    但有时我还是会使用下面的方法作为替代方法。

    在您的OnCreate 中初始化您的rootView(XML 中的父视图)和您的footerView

    从您的OnCreate 调用此方法- 键盘启动后,您可以更改不需要显示的视图的可见性! 当键盘按下时再次显示它们。

     public void testKeyBoardUp(){
            rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    Rect r = new Rect();
                    rootView.getWindowVisibleDisplayFrame(r);
                    int heightDiff = rel.getRootView().getHeight() - (r.bottom - r.top);
    
                    if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
                        //ok now we know the keyboard is up...
                        whatEverView.setVisibility(View.INVISIBLE);
    
    
                    }else{
                        //ok now we know the keyboard is down...
                        whatEverView.setVisibility(View.VISIBLE);
    
    
                    }
                }
            });
        }
    

    【讨论】:

    • @p32929 这个答案对您有帮助吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-11
    • 1970-01-01
    • 2021-03-22
    • 2012-10-12
    相关资源
    最近更新 更多