我试图让 Android 在获得焦点时选择 EditText 字段中的所有文本。我在布局中使用此属性(在两个字段上):
android:selectAllOnFocus="true"
我不确定这是否相关,但为了将光标移至第一个可编辑字段(前面还有一个禁用字段),我使用以下命令:
quizStartNum.setFocusable(true);
quizStartNum.requestFocus();
但是,虽然首次显示布局时光标确实移动到所需字段,但文本不会突出显示;相反,光标最终位于文本的左侧,这是默认行为。如果我通过触摸移动到第二个字段,则会根据需要选择所有文本。然后,如果我移回第一个字段,再次触摸它,文本也会被完全选择。我希望从一开始就拥有这种行为。有办法做到这一点吗?
I'm trying to get Android to select all the text in an EditText field when it gets the focus. I'm using this attribute in the layout (on both fields):
android:selectAllOnFocus="true"
I'm not sure if this is relevant, but to get the cursor to the first editable field (there's also a disabled field before it), I'm using the following commands:
quizStartNum.setFocusable(true);
quizStartNum.requestFocus();
But, while the cursor does move to the desired field when the layout is first displayed, the text doesn't get highlighted; instead the cursor ends up to the left of the text, the default behavior. If I move to the second field by touching it, all the text is selected as desired. Then, if I move back to the first field, again by touching it, the text is also completely selected. I would like to have the behavior right from the start. Is there a way to do this?
发布评论
评论(6)
如果
android:selectAllOnFocus="true"
不起作用,请尝试在该特定 EditText 上调用setSelectAllOnFocus(true)
。如果这也不起作用,这是上一篇 SO 帖子中的另一个解决方法。
If
android:selectAllOnFocus="true"
does not work, try callingsetSelectAllOnFocus(true)
on that particular EditText.If that doesn't work either, this is another workaround from a previous SO post.
我遇到了类似的问题,并且
android:selectAllOnFocus="true"
对我不起作用。原因是我在显示 EditText 之前以编程方式请求焦点。因此,如果您要对 AlertDialog 中的 EditText 执行此操作,请确保在请求焦点到 EditText 之前显示它。
I had a similar issue and
android:selectAllOnFocus="true"
did not work for me.The reason was that i was programatically requesting focus to the EditText before it was displayed. So if you are doing this for a EditText in an AlertDialog make sure you show it before you request focus to the EditText.
EditText 显示后应该获得焦点。
以下锻炼对我有用,
EditText should be focussed after it displayed.
Following workout worked for me,
尝试删除焦点命令。它们不是必需的,Android 应该自动关注第一个字段?
Try removing your focus commands. They aren't necessary, Android should focus on the first field automatically?
我还注意到 android:selectAllOnFocus="true" 似乎不起作用,我用鼠标在模拟器中选择 EditText,但如果我用手指触摸它,就像在手机上一样,它就起作用了。如果您的计算机上没有触摸屏,您可能需要在物理设备上安装应用程序才能进行测试。
在这种情况下 Android Studio 可能无法识别鼠标
I also noticed that with android:selectAllOnFocus="true" seemed not to work, I used the mouse to select EditText in the emulator, but if I used my finger and touched it, as if on the phone, it worked. If you don't have a touch screen on your computer you may have to install your app on a physical device to test it.
Android Studio may not recognize the mouse in this case
我将参考这篇旧文章。
如果您只想让
EditText
显示提示(“此处内容”),您可能需要使用 Android 的 XML 属性hint
(链接)。I'll refer to this older post.
If you just want your
EditText
to show a hint (for "what goes in here"), you might want to use the Android's XML-attributehint
(link).