How to hide auto show keyboard on layout gone

2019-08-12 15:53发布

问题:

i've a layout like:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@android:color/background_dark"
    android:weightSum="1" >

    <LinearLayout
        android:id="@+id/layout_splash"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="0.3" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:src="@drawable/logo" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/testoCaricamento"
            android:layout_gravity="center"
            android:textSize="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

        <LinearLayout
            android:id="@+id/layoutNomeTablet"
            android:visibility="gone"
            android:layout_width="match_parent"
            android:layout_marginTop="40dp"
            android:layout_height="wrap_content"
            android:weightSum="1" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="right"
                android:textSize="20dp"
                android:layout_marginRight="15dp"
                android:layout_weight="0.2"
                android:text="Nome tablet" />

            <EditText
                android:id="@+id/editText1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.5" >

                <requestFocus />
            </EditText>

            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="30dp"
                android:layout_weight="0.1"
                android:text="Salva" />

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

When my splashscreen is shown, keyboard appears even if the layout that contain the EditText has visibility gone. how can i show keyboard only when edittext is visible and focused?

回答1:

Try making this change in your EditText.

<EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight=""
</EditText>