如何在android中自动移动到下一个编辑文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12470067/
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
How to automatically move to the next edit text in android
提问by user1667307
I have a couple of edit text boxes on a single line. After the user types in a specific number of characters in the first I want to automatically move to the next edit text. How do I achieve this?
我在一行中有几个编辑文本框。用户在第一个输入特定数量的字符后,我想自动移动到下一个编辑文本。我如何实现这一目标?
回答by Shekhar Chikara
You can achieve this by using the Text Watcher
class and then set the focus on the next EditText
in the OnTextChanged()
method of the TextWatcher.
您可以通过使用Text Watcher
该类来实现这一点,然后在 TextWatcherEditText
的OnTextChanged()
方法中将焦点设置在 next 上。
In your case, since you have two Edit Texts, say et1
and et2
. You can try out the following code:-
在您的情况下,由于您有两个编辑文本,例如et1
和et2
。您可以尝试以下代码:-
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
}
});
I have not checked out the code myself, but I hope this will help you solve your problem.
我没有亲自检查代码,但我希望这能帮助您解决问题。
回答by Bill Farmer
There is a simpler way to do this which doesn't involve knowledge of the ids of the EditText views. For use with android:maxLength="1".
有一种更简单的方法可以做到这一点,它不涉及 EditText 视图的 id 知识。与 android:maxLength="1" 一起使用。
// onTextChanged
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
TextView text = (TextView)getCurrentFocus();
if (text != null && text.length() > 0)
{
View next = text.focusSearch(View.FOCUS_RIGHT); // or FOCUS_FORWARD
if (next != null)
next.requestFocus();
doSearch(); // Or whatever
}
}
// afterTextChanged
@Override
public void afterTextChanged(Editable s) {}
// beforeTextChanged
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {}
回答by Huy Tower
The accepted answer is good.
接受的答案很好。
I provide new way if you are using RxJava + RxBinding library.
如果您使用 RxJava + RxBinding 库,我会提供新方法。
The coding indicate 4 input Edit Text need verify (My case is Active code SMS via Phone - Phone Verify)
编码表示 4 个输入编辑文本需要验证(我的情况是通过电话激活代码短信 - 电话验证)
// Show button Active code when enough fields active code
Observable<Boolean> mObsPhoneVerify1 = RxTextView.textChanges(db.etPhoneVerify1)
.observeOn(AndroidSchedulers.mainThread())
.map(charSequence -> {
db.etPhoneVerify2.requestFocus();
return charSequence != null && !charSequence.toString().equals("");
}
);
Observable<Boolean> mObsPhoneVerify2 = RxTextView.textChanges(db.etPhoneVerify2)
.observeOn(AndroidSchedulers.mainThread())
.map(charSequence -> {
db.etPhoneVerify3.requestFocus();
return charSequence != null && !charSequence.toString().equals("");
});
Observable<Boolean> mObsPhoneVerify3 = RxTextView.textChanges(db.etPhoneVerify3)
.observeOn(AndroidSchedulers.mainThread())
.map(charSequence -> {
db.etPhoneVerify4.requestFocus();
return charSequence != null && !charSequence.toString().equals("");
});
Observable<Boolean> mObsPhoneVerify4 = RxTextView.textChanges(db.etPhoneVerify4)
.observeOn(AndroidSchedulers.mainThread())
.map(charSequence -> {
db.etPhoneVerify1.requestFocus();
hideKeyboard();
return charSequence != null && !charSequence.toString().equals("");
});
disposable = Observable
.combineLatest(mObsPhoneVerify1, mObsPhoneVerify2, mObsPhoneVerify3, mObsPhoneVerify4,
(PhoneVerify1, PhoneVerify2, PhoneVerify3, PhoneVerify4)
-> PhoneVerify1 && PhoneVerify2 && PhoneVerify3 && PhoneVerify4)
.compose(regisObserver(false))
.subscribe(aBoolean -> {
db.btnActiveCode.setEnabled(aBoolean);
});
busDisposables.add(disposable);