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

在我的 Android应用程序中,我有一个Activity,用户可以在其中添加一些文本到图像.按下启动此按钮的按钮时,屏幕底部会出现TextInput,并显示“保存”按钮.

相关配置如下所示:

<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和Save按钮.这只发生在我的手机上,在7.1.1上,我试过的仿真器变体似乎正常工作.有趣的是,尽管它们被遮挡了,它们仍然可以工作 – 如果我在键盘上键入内容然后按下Save按钮的位置,它会按预期保存文本.

此屏幕截图显示了操作中的问题:

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

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

我在previous questions最接近的事情表明android:inputType =“textNoSuggestions”可能会有所帮助,但它似乎没有任何区别 – 这似乎不是一个建议框,只是一个任意的黑色矩形.

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

解决方法

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

解决方案是将android:fitsSystemWindows =“true”添加到视图根的FrameLayout.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...