我在一行上有几个编辑文本框。当用户在第一个文本中键入特定数量的字符后,我希望自动移动到下一个编辑文本。我该如何实现这一点?
发布于 2012-09-18 12:20:33
可以通过使用Text Watcher
类来实现这一点,然后在EditText
的OnTextChanged()
方法中将焦点设置在下一个TextWatcher上。
在您的例子中,因为您有两个编辑文本,比如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
}
});
我自己还没有签出代码,但我希望这能帮助你解决你的问题。
发布于 2017-01-30 02:46:02
有一种更简单的方法可以做到这一点,它不需要知道EditText视图的is。用于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) {}
发布于 2017-04-20 11:29:24
公认的答案是好的。
如果你正在使用RxJava + RxBinding库,我提供了新的方法。
编码表明需要验证4个输入编辑文本(我的案例是通过电话-电话验证的激活代码SMS )
// 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);
https://stackoverflow.com/questions/12470067
复制相似问题