Android:选择所有文字集中在EditField上 [英] Android: select all text on focus on EditField
问题描述
我正在尝试让Android在获得焦点时选择EditText字段中的所有文本。我在布局中(在两个字段上)都使用了此属性:
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?
推荐答案
如果 android:selectAllOnFocus = true
不起作用,请尝试在该特定EditText上调用 setSelectAllOnFocus(true)
。
If android:selectAllOnFocus="true"
does not work, try calling setSelectAllOnFocus(true)
on that particular EditText.
EditText dummy = ...
dummy.setOnFocusChangedListener(new OnFocusChangedListener(){
public void onFocusChange(View v, boolean hasFocus){
if (hasFocus) && (isDummyText())
((EditText)v).selectAll();
}
});
这篇关于Android:选择所有文字集中在EditField上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!