将 TextInput 放在 Android 键盘上方而不绘制黑色矩形?
Place a TextInput above the Android keyboard without it drawing a black rectangle?
在我的 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>
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>
activity 以图像路径开始,然后将其加载到图像中。当按下注释按钮时,它会显示文本框。
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
和保存按钮。这只发生在我的 phone 上,它是在 7.1.1 上,我尝试过的模拟器变体似乎可以正常工作。有趣的是,尽管它们被遮盖了,但它们仍然有效 - 如果我在键盘上键入内容然后按下保存按钮应该在的位置,它会按预期保存文本。
此屏幕截图显示了实际问题:
通过更改配置文件周围的设置,我发现了不显示黑色矩形的情况,但在每种情况下我都发现它也隐藏了 EditText
组件,甚至在键盘关闭后也是如此我再也没有看到它,让 activity 处于一种奇怪的状态,无法按“保存”按钮。
如果键盘一直打开,黑色矩形只会在 EditText
获得焦点后出现,在此之前键盘看起来很正常。
我在 中找到的最接近的东西表明 android:inputType="textNoSuggestions"
可能有帮助,但它似乎没有任何区别 - 这似乎不是建议框,只是一个任意黑色矩形。
我需要做什么才能停止我的键盘在我的输入框上方绘制一个毫无意义的黑色矩形,或者,如果我以某种方式以错误的方式接近它,我需要做什么才能让输入框允许用户看到他们正在写的文本,然后在他们认为完成后保存它?
问题原来与屏幕底部的 Android 软件按钮有关,而不是与键盘本身有关 - 由于某种原因,它们导致布局在每次按下键盘时错误地测量屏幕被打开。
解决方案是将 android:fitsSystemWindows="true"
添加到视图根目录的 FrameLayout
。
在我的 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>
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>
activity 以图像路径开始,然后将其加载到图像中。当按下注释按钮时,它会显示文本框。
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
和保存按钮。这只发生在我的 phone 上,它是在 7.1.1 上,我尝试过的模拟器变体似乎可以正常工作。有趣的是,尽管它们被遮盖了,但它们仍然有效 - 如果我在键盘上键入内容然后按下保存按钮应该在的位置,它会按预期保存文本。
此屏幕截图显示了实际问题:
通过更改配置文件周围的设置,我发现了不显示黑色矩形的情况,但在每种情况下我都发现它也隐藏了 EditText
组件,甚至在键盘关闭后也是如此我再也没有看到它,让 activity 处于一种奇怪的状态,无法按“保存”按钮。
如果键盘一直打开,黑色矩形只会在 EditText
获得焦点后出现,在此之前键盘看起来很正常。
我在 android:inputType="textNoSuggestions"
可能有帮助,但它似乎没有任何区别 - 这似乎不是建议框,只是一个任意黑色矩形。
我需要做什么才能停止我的键盘在我的输入框上方绘制一个毫无意义的黑色矩形,或者,如果我以某种方式以错误的方式接近它,我需要做什么才能让输入框允许用户看到他们正在写的文本,然后在他们认为完成后保存它?
问题原来与屏幕底部的 Android 软件按钮有关,而不是与键盘本身有关 - 由于某种原因,它们导致布局在每次按下键盘时错误地测量屏幕被打开。
解决方案是将 android:fitsSystemWindows="true"
添加到视图根目录的 FrameLayout
。