簡體   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