将TextInput放在Android键盘上方而不绘制黑色矩形?

时间:2018-04-27 17:26:20

标签: java android android-softkeyboard

在我的Android应用中,我有一个Activity,用户可以在其中为图像添加一些文字。按下启动此按钮的按钮时,屏幕底部会出现一个TextInput,其中包含" Save"按钮重叠。

相关配置如下所示:

<activity
            android:name=".ImageEditorActivity"
 android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:label="@string/title_activity_image_editor"
            android:parentActivityName=".MainActivity"
            android:windowSoftInputMode="adjustResize|stateHidden"
            android:theme="@style/FullscreenTheme">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="myApp.MainActivity" />
        </activity>

活动xml看起来像这样 - 我已经删除了几个与此功能无关的额外按钮:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0099cc"
    tools:context=".ImageEditorActivity"
    android:id="@+id/fullscreen_content">
<ScrollView
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:layout_marginTop="50dp"
    android:isScrollContainer="true" >
        <RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:id="@+id/edit_image"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:adjustViewBounds="true"></ImageView>
            <EditText
                android:id="@+id/image_comment"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_marginTop="8dp"
                android:paddingLeft="8dp"
                android:shadowDx="0"
                android:shadowDy="0"
                android:shadowRadius="2"
                android:hint="notes"
                android:text=""
                android:textColor="@android:color/black"
                android:background="@android:color/white"
                android:layout_alignParentBottom="true"
                android:visibility="invisible"
                app:layout_constraintStart_toEndOf="parent"
                app:layout_constraintTop_toBottomOf="parent" />

            <Button
                android:id="@+id/image_comment_save_button"
                android:layout_width="100dp"
                android:layout_height="45dp"
                android:layout_marginBottom="2dp"
                android:layout_alignParentBottom="true"
                android:layout_alignParentRight="true"
                android:text="Save"
                android:visibility="invisible"
                app:layout_constraintStart_toEndOf="parent"
                app:layout_constraintTop_toBottomOf="parent"
                />
        </RelativeLayout>
</ScrollView>
 <Button
            android:id="@+id/note_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/add_note"
            app:layout_constraintBottom_toTopOf="parent"></Button>

活动以图像路径开始,然后加载到图像中。按下音符按钮时,它会显示文本框。

noteButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            writeNotes();
        }
    });

 private void writeNotes() {
        InputMethodManager methodMan = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        if (noteBox.getVisibility() == View.VISIBLE) {
            String text = noteBox.getText().toString();
            if ( 0 < text.trim().length() ) {
                Bitmap image = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
                writeOnImage(image, text);
                imageView.setImageBitmap(image);
                saveImage(image, lastTaken);
                exifData.put(ExifInterface.TAG_USER_COMMENT, text);
            }
            noteBox.setVisibility(View.INVISIBLE);
            noteSaveButton.setVisibility(View.INVISIBLE);
            methodMan.hideSoftInputFromWindow(noteBox.getWindowToken(), 0);
        }
        else {
            noteBox.setText("");
            noteBox.setVisibility(View.VISIBLE);
            noteSaveButton.setVisibility(View.VISIBLE);
            noteBox.requestFocus();
            methodMan.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
        }
    }

问题是当键盘打开时,它上面有一个大的黑色矩形,完全遮盖了EditText和“保存”按钮。这只发生在我的手机上,这是7.1.1,我试过的模拟器变种似乎正常工作。有趣的是,尽管它们被遮挡了,它们仍然可以工作 - 如果我在键盘上键入内容然后按下Save按钮的位置,它会按预期保存文本。

此屏幕截图显示了操作中的问题: The black bar above the keyboard is obscuring my EditText component.

通过更改配置文件周围的设置,我发现没有显示黑色矩形的情况,但在每种情况下我都发现它也隐藏了EditText组件,即使在键盘关闭后我也从不再看一遍,让活动处于一种奇怪的状态,无法按下保存按钮。

如果键盘一直打开,只有在EditText有焦点时才会出现黑色矩形,在此之前键盘看起来正常。

我在previous questions中找到的最接近的事情表明android:inputType="textNoSuggestions"可能有所帮助,但它似乎没有任何区别 - 这似乎不是一个意见箱,只是一个任意的黑色矩形。

我需要做什么来阻止我的键盘在我的输入框上方绘制一个无意义的黑色矩形,或者,如果我以某种方式接近错误的方式,我需要做一个允许用户输入框的方法看到他们正在写的文本,然后在内容完成后保存它?

1 个答案:

答案 0 :(得分:3)

问题原因与屏幕底部的Android软件按钮有关,而不是键盘本身 - 由于某种原因,它们导致布局在键盘打开时不正确地测量屏幕。

解决方案是将android:fitsSystemWindows="true"添加到视图根目录的FrameLayout