Android 软键盘隐藏了一半的 EditText
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20128193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Soft keyboard hides half of EditText
提问by Gintas_
I have a listview and the last list item contains EditText:
我有一个列表视图,最后一个列表项包含 EditText:
<RelativeLayout
android:id="@+id/contentLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="30dp"
android:orientation="vertical" >
<ImageView
android:id="@+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/messageEditText"
android:layout_below="@+id/messageEditText"
android:layout_marginRight="14dp"
android:src="@drawable/test" />
<EditText
android:id="@+id/messageEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/sendImageButton"
android:ems="10"
android:gravity="top"
android:hint="@string/messageEditText"
android:inputType="textMultiLine"
android:minHeight="55dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp" />
<ImageButton
android:id="@+id/sendImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/messageEditText"
android:layout_alignParentRight="true"
android:layout_alignTop="@id/messageEditText"
android:adjustViewBounds="true"
android:maxHeight="55dp"
android:maxWidth="55dp"
android:padding="12dp"
android:scaleType="centerInside"
android:src="@drawable/sendmessage" />
</RelativeLayout>
Half of the EditText is hidden. I also can't scroll listview. Any solution?
一半的 EditText 被隐藏。我也无法滚动列表视图。有什么解决办法吗?
回答by Gintas_
Solution was to set an android:softInputMode
attribute to adjustResize
in Manifest and put layout(not list item layout) inside a ScrollView.
解决方案是在 Manifest 中设置一个android:softInputMode
属性adjustResize
并将布局(不是列表项布局)放在 ScrollView 中。
回答by sham.y
回答by Gal Rom
From my early days as a Android Developer I struggled with the virtual keyboard. I am amazed that Android is still not giving an elegant and clear solution for this.
从我作为 Android 开发人员的早期开始,我就一直在努力使用虚拟键盘。我很惊讶 Android 仍然没有为此提供优雅而清晰的解决方案。
So here is the Workaround that will put this mess behind you. it will work without the ScrollView workaround or giving away your full screen flag.
所以这里是解决方法,它将把这个烂摊子抛在脑后。它可以在没有 ScrollView 解决方法或放弃全屏标志的情况下工作。
- Add this awesome Library to your gradle file:
- 将这个很棒的库添加到您的 gradle 文件中:
compile'net.yslibrary.keyboardvisibilityevent:keyboardvisibilityevent:1.0.1'
编译'net.yslibrary.keyboardvisibilityevent:keyboardvisibilityevent:1.0.1'
- Make sure your activity has the following keyboard settings:
- 确保您的活动具有以下键盘设置:
android:windowSoftInputMode="adjustResize"
android:windowSoftInputMode="adjustResize"
wrap your EditText with a vertical LinearLayout and add a View with Visibility of Gone:
<com.ylimitapp.ylimitadmin.views.NormalFontEditText android:id="@+id/input_et" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textCapSentences|textMultiLine" android:imeOptions="actionSend" android:paddingStart="20dp" android:paddingEnd="40dp" android:paddingTop="10dp" android:maxHeight="120dp" android:adjustViewBounds= "true" android:scrollHorizontally="false" android:textColorHint="#7b7b7b" android:hint="@string/type_your_message" android:background="@drawable/msg_inputfield_bg" android:textColor="@color/black_text_color" android:textSize="15.33sp" /> <View android:id="@+id/keyboard_view" android:layout_width="match_parent" android:layout_height="20dp" android:visibility="gone" /> </LinearLayout>
calculate the screen size so you can calculate the height of the view that pushes the EditText:
private void storeScreenHeightForKeyboardHeightCalculations() { Rect r = new Rect(); View rootview = getActivity().getWindow().getDecorView(); rootview.getWindowVisibleDisplayFrame(r); mOriginalScreenHeight = r.height(); Rect rectangle = new Rect(); Window window = getActivity().getWindow(); window.getDecorView().getWindowVisibleDisplayFrame(rectangle); int statusBarHeight = rectangle.top; int contentViewTop = window.findViewById(Window.ID_ANDROID_CONTENT).getTop(); int titleBarHeight= contentViewTop - statusBarHeight; if (titleBarHeight == 0) { mOriginalScreenHeight -= (24 * Utils.getDensity(getContext())); } }
- Add a Listener for keyboard open and close event and then set the height of the View below the EditText on runtime so we will set the height properly on any device and custom keyboards. then simply make it Visible when the Keyboard is Open:
用垂直的 LinearLayout 包裹你的 EditText 并添加一个可见性消失的视图:
<com.ylimitapp.ylimitadmin.views.NormalFontEditText android:id="@+id/input_et" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textCapSentences|textMultiLine" android:imeOptions="actionSend" android:paddingStart="20dp" android:paddingEnd="40dp" android:paddingTop="10dp" android:maxHeight="120dp" android:adjustViewBounds= "true" android:scrollHorizontally="false" android:textColorHint="#7b7b7b" android:hint="@string/type_your_message" android:background="@drawable/msg_inputfield_bg" android:textColor="@color/black_text_color" android:textSize="15.33sp" /> <View android:id="@+id/keyboard_view" android:layout_width="match_parent" android:layout_height="20dp" android:visibility="gone" /> </LinearLayout>
计算屏幕大小,以便您可以计算推动 EditText 的视图的高度:
private void storeScreenHeightForKeyboardHeightCalculations() { Rect r = new Rect(); View rootview = getActivity().getWindow().getDecorView(); rootview.getWindowVisibleDisplayFrame(r); mOriginalScreenHeight = r.height(); Rect rectangle = new Rect(); Window window = getActivity().getWindow(); window.getDecorView().getWindowVisibleDisplayFrame(rectangle); int statusBarHeight = rectangle.top; int contentViewTop = window.findViewById(Window.ID_ANDROID_CONTENT).getTop(); int titleBarHeight= contentViewTop - statusBarHeight; if (titleBarHeight == 0) { mOriginalScreenHeight -= (24 * Utils.getDensity(getContext())); } }
- 为键盘打开和关闭事件添加一个侦听器,然后在运行时在 EditText 下方设置视图的高度,这样我们就可以在任何设备和自定义键盘上正确设置高度。然后只需在键盘打开时使其可见:
private void addkeyBoardlistener() { KeyboardVisibilityEvent.setEventListener( getActivity(), new KeyboardVisibilityEventListener() { @Override public void onVisibilityChanged(boolean isOpen) { if (isOpen) { Rect r = new Rect(); View rootview = getActivity().getWindow().getDecorView(); // this = activity rootview.getWindowVisibleDisplayFrame(r); int keyboardHeight = (mOriginalScreenHeight - r.height()); LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) keyboard_view.getLayoutParams(); params.height = (int) ((keyboardHeight + 5 * Utils.getDensity(getContext()))); keyboard_view.setLayoutParams(params); keyboard_view.setVisibility(View.VISIBLE); } else { keyboard_view.setVisibility(View.GONE); } } }); }
private void addkeyBoardlistener() { KeyboardVisibilityEvent.setEventListener( getActivity(), new KeyboardVisibilityEventListener() { @Override public void onVisibilityChanged(boolean isOpen) { if (isOpen) { Rect r = new Rect(); View rootview = getActivity().getWindow().getDecorView(); // this = activity rootview.getWindowVisibleDisplayFrame(r); int keyboardHeight = (mOriginalScreenHeight - r.height()); LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) keyboard_view.getLayoutParams(); params.height = (int) ((keyboardHeight + 5 * Utils.getDensity(getContext()))); keyboard_view.setLayoutParams(params); keyboard_view.setVisibility(View.VISIBLE); } else { keyboard_view.setVisibility(View.GONE); } } }); }
This is the result:
这是结果:
回答by it's me
Disable the keyboard on window startup
在窗口启动时禁用键盘
this .getWindow().setSoftInputMode(WindowManager.LayoutParams. SOFT_INPUT_STATE_ALWAYS_HIDDEN );
回答by Bryan Herbst
Set an android:softInputMode
attribute for your Activity element in your Manifest.
android:softInputMode
在您的清单中为您的 Activity 元素设置一个属性。
See the Manifest documentationfor a full list of valid values and their effect. The ones of particular interest to you will likely be adjustPan
and adjustResize
.
有关有效值及其影响的完整列表,请参阅清单文档。您特别感兴趣的可能是adjustPan
和adjustResize
。