简体   繁体   English

Android-重点突出的EditTexts和布局行为

[英]Android - Focusable EditTexts and Layout Behaviour

Sorry for the vague title. 抱歉,标题含糊。 I am having some strange behaviour creating a simple log-in page. 我在创建一个简单的登录页面时遇到了一些奇怪的行为。 I have two Edit-Texts in my layout (Username, Password). 我的布局中有两个“编辑文本”(用户名,密码)。 I am saving their state between orientation changes using some global booleans and the saved/restoe instance state methods along with EditText.RequestFocus();. 我使用一些全局布尔值和保存的/ restoe实例状态方法以及EditText.RequestFocus();来保存方向更改之间的状态。

My issue is, this all works perfeclty fine if I have this flag in my passords EditText layout: 我的问题是,如果我在passords EditText布局中有此标志,则所有这些都可以正常运行:

android:scrollbars="vertical" android:scrollbars =“ vertical”

When the view loads, the username is set to focused (automatically by Android it seems) and my code to retain the focus works perfectly. 当视图加载时,用户名设置为焦点(看来是由Android自动实现),而我保留焦点的代码也可以完美地工作。 As well as this when the soft keyboard appears, it neatly resizes my layout to incorperate the change. 不仅如此,在出现软键盘时,它还会巧妙地调整我的布局大小以合并更改。

However, after removing that flag, this all breaks. 但是,删除该标志后,所有操作都会中断。 No EditText is set to focused when the activity loads. 活动加载时,没有将EditText设置为焦点。 My code to retain focusable on orientation change does not work and most annoyingly, the keyboard blocks some of the layout instead of resizing it. 我的代码始终专注于方向更改,因此无法正常工作,最烦人的是,键盘会阻止某些布局,而不是调整其大小。

I don't want to keep this useless flag in my layout, as I am worried this behaviour might change between version and the app might not work for other devices. 我不想在布局中保留此无用的标志,因为我担心此行为可能会在版本之间发生变化,而该应用程序可能不适用于其他设备。 Any help or suggestions? 有什么帮助或建议吗?

Thanks, 谢谢,

Kevin. 凯文

Whilst this isn't a total answer to my question, I thought I would quickly post how I got around this real quick: 尽管这不是我所提问题的全部答案,但我认为我会尽快发布如何快速解决这个问题:

Placing this in my manifest (for the activity i wanted): android:windowSoftInputMode="adjustResize" 将此放置在我的清单中(针对我想要的活动):android:windowSoftInputMode =“ adjustResize”

This seemed to do pretty much the same thing as what the scrollbars were doing - I still have no explanation of that odd behaviour so if anybody has a clue I'd be interested to know. 这似乎与滚动条所做的事情几乎相同-我仍然无法解释这种奇怪的行为,因此,如果有人有我想知道的线索。 However I believe this is the correct way of handling this situation anyway. 但是,我认为这是处理这种情况的正确方法。

I am clueless as to why my code to save state would work one way but not the other. 对于为什么我的状态保存代码只能以一种方式而不是其他方式起作用,我一无所知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM