Android 软键盘:如何操作键盘上的视图开/关
我有一个布局,顶部有一个大的 EditText 视图,底部有一堆按钮。当使用 adjust_resize 激活/停用 ime 时,EditText 会收缩和扩展。底部的按钮被推到时间上方。
我想在 ime 显示时隐藏这些按钮,以便为 EditText 视图提供足够的空间。
到目前为止,我已尝试以下操作:
子类化 EditText 并为活动提供在视图的 OnSizeChanged 上注册回调的选项。
使用此回调将按钮(实际上是布局容器)的可见性更改为 GONE。
这个工作正常,并且当 ime 弹出时隐藏按钮。 但是,EditText 不会扩展到新的可用空间。 此外,当 ime 关闭时,EditText 字段现在比原来大,将(现在显示的)按钮推到屏幕之外。
我还应该补充一点,当在视图中输入第一个字母并且 ime 显示单词选项时,屏幕会重新绘制,并且 EditText 会填充空白区域。
关于如何让它发挥作用有什么想法吗? 或者更好的是,是否有更简单的解决方案可以满足我的要求?
谢谢...
注意:在我看来,滚动不是一个好的选择。
I have a layout which has one large EditText view at the top + a bunch of buttons at the bottom. The EditText is made to shrink and expand when the ime is activated/deactivated by using adjust_resize. The buttons at the bottom are pushed up above the ime.
I would like to hide these buttons when the ime displays, to provide enough space for the EditText view.
I have so far tried the following:
subclassed EditText and provided the activity the option to register a callback on the view's OnSizeChanged.
Used this callback to change the visibility of the buttons (actually the layout container) to GONE.
This work OK and does hide the buttons when the ime pops up.
However, the EditText does not expand into the new available space.
Furthermore, when the ime is disposed off, the EditText field is now bigger than it was originally, pushing (the now showing) buttons outside the screen.
I should also add that when typing the first letter into the view, and the ime displays the word options, the screen is redrawn and the EditText fills the vacant space.
Any ideas on how to get this to work?
Or even better, is there a simpler solution to my requirement?
Thanks...
NB: In my view, scrolling is not a good option.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通过稍微更改上述方法来实现此目的:
用 FrameLayout 包裹整个布局
子类化 FrameLayout 并为活动提供注册回调的选项布局的 OnMeasure
这使活动有机会在测量视图之前更改视图的可见性。
我仍然很高兴听到更简单的解决方案,特别是在确定键盘当前是否可见方面。
(dumpsys 窗口显示此信息。我们可以轻松获取它吗?)
I got this to work by changing the above method a bit:
Wrapped the entire layout with a FrameLayout
subclassed the FrameLayout and provided the activity the option to register a callback on the layout's OnMeasure
This gives the activity a chance to change visibility of views before these are measured.
I would still be very happy to hear about simpler solutions, especially in regards to figuring out whether the keyboard is currently visible or not.
(dumpsys window shows this information. Can we easily get to it?)
您是否尝试过调用
myView.invalidade()
?我使用的是 GONE 属性,但后来更改为
button.setVisibility(View.INVISIBLE);
因为我的屏幕上没有任何其他内容。在调整大小时,您需要在键盘消失时再次检查并再次显示按钮。
Have you tried to call
myView.invalidade()
?I was using the GONE property, but then changed to
button.setVisibility(View.INVISIBLE);
because I don't have any other stuff on my screen.on the adjust_resize, you will need to check again when the keyboard has gone and show the buttons again.