繁体   English   中英

键盘弹出时如何隐藏小部件?

[英]How to hide widget when keyboard pops?

我在同一视图上有两个ViewPager,其中weight = 70/30。 问题是当键盘弹出时,两个视图的权重保持不变,并且不显示我的EditText。

在聚焦之前,第二个ViewPager中的最后一个EditText

不显示

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fab="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="true"
tools:context="com.suez.memboard.fragments.formWaterQuality">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="100"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight="30">

        <com.suez.memboard.controls.TextView
            android:id="@+id/path"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:textAppearance="@style/FreeTextBold" />

        <LinearLayout
            android:id="@+id/date_picker"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp">

            <com.suez.memboard.controls.TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="forms.label.intervention_date"
                android:textAppearance="@style/FreeText"
                android:textSize="15sp" />

            <com.suez.memboard.controls.TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=" : "
                android:textAppearance="@style/FreeText"
                android:textSize="15sp" />

            <com.suez.memboard.controls.DateTimeText
                android:id="@+id/date"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.7"
                android:drawableRight="@drawable/ic_date_range_black_24dp"
                android:focusable="false"
                android:hint="forms.label.intervention_date"
                fab:type="date" />
        </LinearLayout>

        <Spinner
            android:id="@+id/spin_location"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

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

                <android.support.design.widget.TabLayout
                    android:id="@+id/tabA"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="@color/suez_blue_1"
                    app:tabGravity="fill" />

                <ImageButton
                    android:id="@+id/buttonAdd"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@color/suez_blue_1"
                    android:src="@drawable/ic_add_box_white_48dp" />
            </LinearLayout>

            <android.support.v4.view.ViewPager
                android:id="@+id/viewA"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight="70">

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

            <android.support.design.widget.TabLayout
                android:id="@+id/tabB"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/suez_blue_1"
                app:tabGravity="fill" />
        </LinearLayout>

        <android.support.v4.view.ViewPager
            android:id="@+id/viewB"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>


</LinearLayout>

<FAB ....>

我认为我的最后一个解决方案是在键盘弹出时使选择器消失在顶部。 我怎样才能做到这一点?

尝试在父视图中使用ScrollView ,例如-

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusableInTouchMode="true">

    <ScrollView
        android:id="@+id/scroll_view_parent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:fillViewport="true"
        android:scrollbars="none">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                ..................................
                type your code here

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/colorWhite"
                android:orientation="vertical">

                ..................................
                type your code here

            </LinearLayout>

        </LinearLayout>

    </ScrollView>

</RelativeLayout>

希望这对您有帮助:

public class YourActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_activity);

    attachKeyboardListeners();
}

@Override
protected void onShowKeyboard(int keyboardHeight) {
    // when keyboard is shown
    yourWidget.setVisibility(View.GONE);
}

@Override
protected void onHideKeyboard() {
    // when keyboard is hidden
    yourWidget.setVisibility(View.VISIBLE);
 }
}

您可以在打开和关闭键盘时设置监听器。

// ContentView is the root view of the layout of this activity/fragment    
contentView.getViewTreeObserver().addOnGlobalLayoutListener(
    new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {

            Rect r = new Rect();
            contentView.getWindowVisibleDisplayFrame(r);
            int screenHeight = contentView.getRootView().getHeight();

            // r.bottom is the position above soft keypad or device button.
            // if keypad is shown, the r.bottom is smaller than that before.
            int keypadHeight = screenHeight - r.bottom;

            Log.d(TAG, "keypadHeight = " + keypadHeight);

            if (keypadHeight > screenHeight * 0.15) { // 0.15 ratio is perhaps enough to determine keypad height.
                // keyboard is opened
            } else {
                // keyboard is closed
            }
        }
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM