软件键盘删除视图

时间:2017-09-23 20:19:05

标签: android keyboard

我无法在Google上找到关于我的问题的任何内容:

单击EditText后打开软件键盘时,基础视图SOMETIMES将变为不可见/消失。它只发生在每次点击几次,主要是当键盘打开时我打开一个AlertDialog(即使我在打开对话框之前以编程方式关闭键盘)。

如果我点击任何其他内容,例如EditText(它不一定要更改视图),它们会重新出现。

clipChildren设置为false。

我该如何解决这个问题?

编辑:我刚刚意识到我有一个无用的LinearLayout作为我的ScrollLayout的ParentView。忽略

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="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:background="@color/colorBackground"
android:clickable="true"
android:fitsSystemWindows="true"
android:orientation="vertical">



<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorBackground"
        android:clipChildren="false"
        android:orientation="vertical"
        android:padding="@dimen/activity_vertical_margin"


        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_marginTop="16dp"
            android:gravity="center_vertical"
            android:text="@string/icon"
            android:textSize="@dimen/text_size_medium"
            app:layout_constraintBottom_toBottomOf="@+id/task_icon"
            app:layout_constraintTop_toBottomOf="@+id/edit_text_task_name"
            app:layout_constraintVertical_bias="0.0"
            app:layout_constraintStart_toStartOf="parent" />

        <EditText
            android:id="@+id/edit_text_task_name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:hint="@string/edit_name"
            android:imeOptions="actionDone"
            android:inputType="text"
            android:maxLines="1"
            android:textColor="@android:color/white"
            android:textColorHint="@android:color/white"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:id="@+id/task_icon"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
            android:contentDescription="@string/icon"
            app:layout_constraintStart_toEndOf="@+id/textView2"
            app:layout_constraintTop_toBottomOf="@+id/edit_text_task_name"
            app:srcCompat="@drawable/ic_clock_white" />

        <android.support.v7.widget.AppCompatSpinner
            android:id="@+id/task_icon_spinner"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            app:backgroundTint="@android:color/white"
            app:layout_constraintBottom_toBottomOf="@+id/task_icon"
            app:layout_constraintStart_toEndOf="@+id/task_icon"
            app:layout_constraintTop_toTopOf="@+id/task_icon" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="48dp"

            android:layout_marginTop="16dp"
            android:gravity="center_vertical"
            android:text="@string/type"
            android:textSize="@dimen/text_size_medium"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView2" />

        <RadioGroup
            android:id="@+id/radioGroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            app:layout_constraintStart_toEndOf="@+id/textView3"
            app:layout_constraintTop_toTopOf="@+id/textView3">

            <android.support.v7.widget.AppCompatRadioButton
                android:id="@+id/fixed_time_button"
                android:layout_width="wrap_content"
                android:layout_height="48dp"
                android:checked="true"
                android:text="@string/fixed_time"
                android:textColor="@android:color/white" />

            <android.support.v7.widget.AppCompatRadioButton
                android:id="@+id/before_bed_button"
                android:layout_width="wrap_content"
                android:layout_height="48dp"
                android:layout_weight="1"
                android:text="@string/before_bed"

                android:textColor="@android:color/white" />

            <android.support.v7.widget.AppCompatRadioButton
                android:id="@+id/time_passed_button"
                android:layout_width="wrap_content"
                android:layout_height="48dp"
                android:layout_weight="1"
                android:text="@string/time_passed"

                android:textColor="@android:color/white" />
        </RadioGroup>

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="48dp"
            android:layout_marginTop="16dp"
            android:gravity="center_vertical"
            android:text="@string/due"
            android:textSize="@dimen/text_size_medium"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/radioGroup"
            />

        <ImageView
            android:id="@+id/time_picker_image"
            android:layout_width="24dp"
            android:layout_height="48dp"
            android:layout_marginStart="16dp"
            app:layout_constraintBottom_toBottomOf="@+id/textView4"
            app:layout_constraintStart_toEndOf="@+id/textView4"
            app:layout_constraintTop_toTopOf="@+id/textView4"
            app:layout_constraintVertical_bias="0.0"
            app:srcCompat="@drawable/ic_clock_white"
            tools:ignore="ContentDescription" />

        <TextView
            android:id="@+id/time_picker_text"
            android:layout_width="wrap_content"
            android:layout_height="48dp"
            android:gravity="center_vertical"
            android:paddingStart="4dp"
            app:layout_constraintBottom_toBottomOf="@+id/time_picker_image"
            app:layout_constraintStart_toEndOf="@+id/time_picker_image"
            app:layout_constraintTop_toTopOf="@+id/time_picker_image"
            tools:text="at exactly 20:00"
            tools:ignore="RtlSymmetry" />

        <android.support.v7.widget.AppCompatSpinner
            android:id="@+id/time_picker_spinner"
            android:layout_width="wrap_content"
            android:layout_height="48dp"

            app:backgroundTint="@android:color/white"
            app:layout_constraintBottom_toBottomOf="@+id/time_picker_text"
            app:layout_constraintStart_toEndOf="@+id/time_picker_text"
            app:layout_constraintTop_toTopOf="@+id/time_picker_text" />

        <TextView
            android:id="@+id/sleep_time_explanation"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawablePadding="4dp"
            android:text="@string/sleep_time_settings_explanation"
            android:textColor="@color/yellow"
            android:visibility="gone"
            app:layout_constraintStart_toStartOf="@+id/textView4"
            app:layout_constraintTop_toBottomOf="@+id/time_picker_text"
            tools:drawableStart="@drawable/ic_lightbulb_yellow_small"
            tools:visibility="visible" />

        <TextView
            android:id="@+id/textView5"
            android:layout_width="wrap_content"
            android:layout_height="48dp"
            android:layout_marginTop="16dp"
            android:gravity="center_vertical"
            android:text="@string/reminder"
            android:textSize="@dimen/text_size_medium"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/sleep_time_explanation" />

        <RadioGroup
            android:id="@+id/radioGroup2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="0dp"
            app:layout_constraintStart_toEndOf="@+id/textView5"
            app:layout_constraintTop_toTopOf="@+id/textView5">

            <android.support.v7.widget.AppCompatRadioButton
                android:id="@+id/no_reminder_button"
                android:layout_width="wrap_content"
                android:layout_height="48dp"
                android:layout_weight="1"
                android:checked="true"
                android:text="@string/no_reminder"
                android:textColor="@android:color/white" />

            <android.support.v7.widget.AppCompatRadioButton
                android:id="@+id/remind_me_button"
                android:layout_width="wrap_content"
                android:layout_height="48dp"
                android:layout_weight="1"
                android:text="@string/remind_me"
                android:textColor="@android:color/white" />

        </RadioGroup>

        <ImageView
            android:id="@+id/reminder_picker_image"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:layout_weight="1"
            android:padding="12dp"
            android:visibility="gone"
            app:layout_constraintStart_toStartOf="@+id/radioGroup2"
            app:layout_constraintTop_toBottomOf="@+id/radioGroup2"
            app:srcCompat="@drawable/ic_bell_white"
            tools:ignore="ContentDescription"
            tools:visibility="visible" />

        <TextView
            android:id="@+id/reminder_picker_text"
            android:layout_width="wrap_content"
            android:layout_height="48dp"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:visibility="gone"
            app:layout_constraintBottom_toBottomOf="@+id/reminder_picker_image"
            app:layout_constraintStart_toEndOf="@+id/reminder_picker_image"
            app:layout_constraintTop_toTopOf="@+id/reminder_picker_image"
            tools:text="1:00h before"
            tools:visibility="visible" />

        <android.support.v7.widget.AppCompatSpinner
            android:id="@+id/reminder_picker_spinner"
            android:layout_width="wrap_content"
            android:layout_height="48dp"
            android:visibility="gone"
            app:backgroundTint="@android:color/white"
            app:layout_constraintBottom_toBottomOf="@+id/reminder_picker_text"
            app:layout_constraintStart_toEndOf="@+id/reminder_picker_text"
            app:layout_constraintTop_toTopOf="@+id/reminder_picker_text"
            tools:visibility="visible" />

    </android.support.constraint.ConstraintLayout>
</ScrollView>

1 个答案:

答案 0 :(得分:0)

希望这个答案可以帮到你。

 FrameLayout layout = (FrameLayout)findViewById(R.id.Frame);
        layout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                InputMethodManager input = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                input.hideSoftInputFromWindow(v.getWindowToken(),0);

                return false;
            }
        });

或者您可以使用代码通过按钮隐藏软键盘。

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            InputMethodManager input = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            input.hideSoftInputFromWindow(editText.getWindowToken(),0);

        }
    });