问题描述
通常,单击视图时,将文本光标设置在您单击的位置附近.
我尝试将其始终设置为末尾(过去的字符),但是除非动作延迟,否则它什么也不做.
以下:
Normally, when the view is clicked, the text cursor is set near the place you clicked on.
I try to set it always to the end (past the last character), but it does nothing unless the action is delayed.
The following:
new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus) {
EditText et = (EditText) v;
et.setSelection(et.getText().length());
}
}
});
不起作用.延迟setSelection
部分:
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
et.setSelection(et.getText().length());
}
}, 40);
使它起作用,但这是不好的做法.
makes it work, but is bad practice.
有哪些更好的选择? OnClickListener
和OnTouchListener
(ACTION_DOWN和ACTION_UP都)也无济于事.
What better alternatives are there? OnClickListener
and OnTouchListener
(ACTION_DOWN and ACTION_UP both) don't help, either.
推荐答案
无需延迟.我们需要等待setSelection
,否则将自动调用完成并执行我的setSelection
.
No need delay. we need to wait setSelection
or else automatically called done and execute my setSelection
.
new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus) {
final EditText et = (EditText) v;
et.post(new Runnable() {
@Override
public void run() {
et.setSelection(et.getText().length());
}
});
}
}
});
这篇关于EditText的onFocusChange中的setSelection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!