Android EditText.setSelection(结束)不起作用?
我使用以下代码在customEditText(Android EditText.setSelection(结束)不起作用?,android,android-layout,Android,Android Layout,我使用以下代码在customEditText(EditText)的末尾设置光标,但没有效果 customEditText.setOnFocusChangeListener(new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) {
EditText
)的末尾设置光标
,但没有效果
customEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
String customEditTextText = customEditText.getText().toString();
int selection = customEditTextText.length();
customEditText.setSelection(selection);
}
}
});
有人对此有什么想法吗?试试这个
customEditText.setSelection(customEditText.getText().length()-1);
试试这个
customEditText.setSelection(customEditText.getText().length());
设置选择时无需将其转换为字符串…我在AlertDialog中遇到了相同的EditText问题。我的解决方案是:
mEditText.post(new Runnable() {
@Override
public void run() {
mEditText.setSelection(mEditText.length());
}
});
这对我有用
mEditText.postDelayed(new Runnable() {
@Override
public void run() {
mEditText.setSelection(mEditText.getText().length());
}
}, 50);
不知道为什么,但需要使用postdayed
使其工作。
只是post
不起作用试试这个
editText.requestFocus();
注意:它会自动将光标设置到编辑文本的最后位置。为了设置要工作的选择,应打开软键盘
if(binding.edtListName.requestFocus()) {
DataManager.showKeyboard(binding.edtListName,context);
}
DataManager类:
public static void showKeyboard(EditText editText, Context context){
editText.requestFocus();
editText.postDelayed(new Runnable(){
@Override public void run(){
InputMethodManager keyboard=(InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(editText,0);
if (editText.getText().toString().length() > 0 ) {
editText.setSelection(0,editText.getText().toString().length());
}
}
}
,400);
}
为我工作:
editText.requestFocus();
editText.setSelection(editText.getText().length());
试试这个customEditText.setSelection(customEditText.getText().length())代码>无需将其转换为字符串..您是否尝试过@Jacob???android.widget.EditText.getText()返回editable我相信它不会给他正确的解决方案,但由于它是customedittext,所以还有一些其他问题,如布局。因此,这是一种尝试性的方法。发布的代码与您评论的代码相同。对不起,与我的代码不同,但与问题中的问题相同。当使用圆角自定义编辑文本时,我也有类似的问题,但奇怪的是光标不可见,但问题是其他的。因为我不能对问题发表评论,所以我必须把它写下来作为回答@埃塞斯让他两个答案都试一下我刚问过你如果你感觉不好很抱歉他想指出课文的结尾。由于0位于文本开头的左侧,因此text.length指向文本的右侧,这就是目标(据我所知),请尝试使用此customEditText.setSelection(customEditText.length())代码>以上这些都不适合我。当片段打开时,我不得不在我的edit text in motion event runnable上使用setSelection方法使软键输入键盘向上滑动!这应该是公认的答案。还有,正确的方法。