移动到android中的下一个EditText
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12488588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Moving to the next EditText in android
提问by user1667307
According to the question and answer posted How to automatically move to the next edit text in android, I used the code to move to the next edit box.
根据发布的问答如何在android中自动移动到下一个编辑文本,我使用代码移动到下一个编辑框。
et1.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start,int before, int count)
{
// TODO Auto-generated method stub
if(et1.getText().toString().length()==size) //size as per your requirement
{
et2.requestFocus();
}
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
The issue I am facing is in case I enter something wrong, then even if the user explicitly request focus , the text box moves to the next one due to the code in the text watcher. Is there anything I can do to prevent this from happening ?
我面临的问题是,如果我输入错误,即使用户明确请求 focus ,由于文本观察器中的代码,文本框也会移动到下一个。我能做些什么来防止这种情况发生?
采纳答案by Hiral Vadodaria
You can check the key pressed for 1st EditText and if it was "Enter" key,then move focus to next EditText.
您可以检查为第一个 EditText 按下的键,如果它是“Enter”键,则将焦点移到下一个 EditText。
This may help you: KeyCode_Enter to next edittext
这可能对您有帮助:KeyCode_Enter 到下一个编辑文本
回答by arshu
It might be very late but I have a solution which is much easier to apply. Just add this code to the edittext field in the layout.xmlfile. Add this to edittext which you want to show a next button instead of a done button. So it will point to the next Edittext field.
可能会很晚,但我有一个更容易应用的解决方案。只需将此代码添加到 layout.xml文件中的 edittext 字段即可。将此添加到要显示下一步按钮而不是完成按钮的edittext 。所以它将指向下一个 Edittext 字段。
android:imeOptions="actionNext"
回答by Daniel López
If you want to focus next EditText on enter pressed you can use those options at XML code to focus next EditText:
如果您想在按下 Enter 时关注下一个 EditText,您可以使用 XML 代码中的这些选项来关注下一个 EditText:
Option 1:
选项1:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionNext"
android:singleLine="true"/>
Option 2:
选项 2:
<EditText
android:id="@+id/et1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:nextFocusForward="@+id/et2"/>
<EditText
android:id="@+id/et2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"/>
回答by UVM
You need to validate user input to meet your expectations. If that is met, then only call et2.requestFocus()
您需要验证用户输入以满足您的期望。如果满足,则只调用et2.requestFocus()
回答by Fabio Guerra
It work for me:
它对我有用:
final EditText code1=((EditText)view.findViewById(R.id.code1));
final EditText code2=((EditText)view.findViewById(R.id.code2));
final EditText code3=((EditText)view.findViewById(R.id.code3));
final EditText code4=((EditText)view.findViewById(R.id.code4));
OnKeyListener key=new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(!((EditText) v).toString().isEmpty())
v.focusSearch(View.FOCUS_RIGHT).requestFocus();
return false;
}
};
code1.setOnKeyListener(key);
code2.setOnKeyListener(key);
code3.setOnKeyListener(key);
回答by MKJParekh
This code works as expected.
此代码按预期工作。
etFName.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(etFName.getText().toString().length()==4 && keyCode == KeyEvent.KEYCODE_BACK)
{
typinget1 = true;
}
else if(etFName.getText().toString().length()==4 && typinget1==true) //size as per your requirement
{
typinget1 = false;
etLName.requestFocus();
}
else if(etFName.getText().toString().length()<=4 && typinget1==false)
{
typinget1 = true;
}
return false;
}
});
回答by Hardik Joshi
You can also check in your if condition that is that right or not..
您还可以检查您的 if 条件是否正确。
if(et1.getText().toString().length()==size) //size as per your requirement
{
if(et1.gettext.toString().equals(somethingwhichyouhavecheck))
{
et2.requestFocus();
}
else
{
}
}
回答by abolfazl bazghandi
this code work for me
这段代码对我有用
private void managePinView(final ArrayList<MaterialEditText> editTexts) {
last = editTexts.size();
last--;
editTexts.get(0).requestFocus();
for (int i = 0; i < editTexts.size(); i++) {
final int finalI = i;
editTexts.get(finalI).addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before,
int count) {
convertText(editTexts.get(finalI), s, this); // i can use this function to change english number to persian number if do not use comment this
int textLength = editTexts.get(finalI).getText().length();
next = finalI + 1;
previous = finalI - 1;
if (textLength >= 1) {
if (next <= last)
editTexts.get(next).requestFocus();
} else {
if (previous >= 0) {
editTexts.get(previous).requestFocus();
}
}
}
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
});
}
}
private void convertText(MaterialEditText editText, CharSequence charSequence, TextWatcher watcher) {
String updatedString = G.convertEnNumberToFi("" + editText.getText().toString().substring(0, editText.length()));
editText.removeTextChangedListener(watcher);
editText.setText(updatedString);
editText.setSelection(charSequence.length());
editText.addTextChangedListener(watcher);
}
回答by nguyencse
This is my approach for editext in recyclerview or listview or whatever.
这是我在 recyclerview 或 listview 或其他任何东西中使用 editext 的方法。
edittext.setOnEditorActionListener { textView, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_NEXT) {
var view = textView.focusSearch(View.FOCUS_DOWN)
while (view != null) {
if (view is EditText) {
view.isCursorVisible = true
return@setOnEditorActionListener view.requestFocus()
}
view = view.focusSearch(View.FOCUS_DOWN)
}
}
false
}
回答by Muhammad Aamir Ali
I have very simple solution for this just put following property in xml edittext control
我有非常简单的解决方案,只需将以下属性放在 xml edittext 控件中
android:inputType="textPersonName"
android:inputType="textPersonName"