如何在android的EditText中设置文本右侧的焦点?
在我的应用程序中,当用户单击或触摸编辑文本视图时,焦点有时设置为开始。例如,如果现有文本为“Hi”。用户想要点击它,并通过添加文本“你好吗?”将其更改为“你好吗?”。但由于焦点在一开始,它变成了“你好吗?嗨”。因此,我始终希望在选中时将焦点设置为文本的最右侧。我该怎么做呢。如果可能的话,请给我一些样品。谢谢你的时间和帮助 您可以明确地将插入符号置于文本中的最后一个位置:如何在android的EditText中设置文本右侧的焦点?,android,android-layout,android-widget,android-edittext,Android,Android Layout,Android Widget,Android Edittext,在我的应用程序中,当用户单击或触摸编辑文本视图时,焦点有时设置为开始。例如,如果现有文本为“Hi”。用户想要点击它,并通过添加文本“你好吗?”将其更改为“你好吗?”。但由于焦点在一开始,它变成了“你好吗?嗨”。因此,我始终希望在选中时将焦点设置为文本的最右侧。我该怎么做呢。如果可能的话,请给我一些样品。谢谢你的时间和帮助 您可以明确地将插入符号置于文本中的最后一个位置: EditText editText = (EditText) findViewById(R.id.textId); int p
EditText editText = (EditText) findViewById(R.id.textId);
int pos = editText.getText().length();
editText.setSelection(pos);
如果您询问的内容更为特殊,可以使用以下代码:
EditText editText = (EditText) findViewById(R.id.textId);
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
editText.setSelection(editText.getText().length());
}
}
});
setOnFocusChangeLister()方法用于检测editText何时接收焦点。上述解决方案不起作用 在这里,我为您提供了一个新的解决方案,用于在android的edittext中将焦点设置到文本的右侧。它工作得很好 我的代码是:
EditText edt_ans = (EditText)findViewById(R.id.edt_answer);
edt_ans.addTextChangedListener(new TextWatcher()
{
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
// TODO Auto-generated method stub
edt_ans.setSelection(edt_ans.getText().length());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,int after)
{
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s)
{
// TODO Auto-generated method stub
}
});
快乐编码……:)一个更突出的方法是,您不需要一直手动设置选择。因此,在布局中使用CustomEditText而不是EditText
public class CustomEditText extends EditText {
public CustomEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* This is a Fix of buggy behavior of android, where after setting text using setText(...), selection remain on
* first character and not at last character.
*/
@Override
public void setText(CharSequence text, BufferType type) {
super.setText(text, type);
if (text != null && ! (text.toString().trim().length() == 0 )) {
final int textLength = text.length();
if (getSelectionEnd() != textLength) {
setSelection(textLength);
}
}
}
}
这对我来说不起作用,但这很有魅力。后文本的工作也发生了变化
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
edittext.requestFocus(edittext.getText().length());
}
在新的api上,您可以使用如下内容:
//buttons setups
private void buttonsSetupForRecommendations() {
button2.setNextFocusRightId(R.id.ok_button);
button2.setNextFocusLeftId(R.id.ok_button);
button2.setNextFocusDownId(R.id.cancel_button);
button2.setNextFocusUpId(R.id.cancel_button);
button.setVisibility(View.GONE);
button2.setText(R.string.cancel_text);
button1.setText(R.string.clear_text);
button1.setNextFocusRightId(R.id.cancel_button);
button1.setNextFocusLeftId(R.id.cancel_button);
button1.setNextFocusUpId(R.id.ok_button);
button1.setNextFocusDownId(R.id.ok_button);
button1.requestFocus();
}
这项工作与按钮和其他项目。您可以在java代码^^或xml中添加此选项
public void ontextchangelistner(){
if(s.toString().startsWith("0")) {
if (s.length() == 2) {
if (s.toString().contains("00")) {
editText.setText(s.toString().substring(1));
int pos = editText.getText().length();
editText.setSelection(pos);
}
}
}
如果在编辑文本中输入第一次零,则显示;但在第二次按零时,不显示该零。
我希望它能对您有所帮助。在
addTextChangedListener()中。
:
您的答案设置了插入符号位置onTextChanged而不是onClick,这正是原始问题所要求的。请尽量不要重复非常老的问题…如果您想请求对视图进行聚焦,然后设置选择,请不要忘记另外调用requestFocus();
@Override
public void afterTextChanged(Editable editable) {
viewHolder.editText.setSelection(viewHolder.editText.getText().length()); //set cursor to the end
}