我在一行上有几个编辑文本框。在用户在第一个输入特定数量的字符后,我想自动移动到下一个编辑文本。我如何实现这一目标?
问问题
38907 次
4 回答
75
您可以通过使用Text Watcher
该类来实现这一点,然后将焦点设置在TextWatcherEditText
的方法中的 next 上。OnTextChanged()
在您的情况下,因为您有两个编辑文本,所以说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
}
});
我自己没有检查过代码,但我希望这能帮助您解决问题。
于 2012-09-18T04:20:32.823 回答
23
有一种更简单的方法可以做到这一点,它不涉及 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) {}
于 2017-01-29T18:46:02.430 回答
4
接受的答案很好。
如果您使用 RxJava + RxBinding 库,我会提供新方法。
编码表示 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);
于 2017-04-20T03:29:24.027 回答
2
如果您有一个要转移焦点的 TextView 列表(例如获取 OTP 输入),那么以下代码将有助于以更少的行数实现它
TextView otp1 = (TextView) root.findViewById(R.id.edit_verify_otp1);
TextView otp2 = (TextView) root.findViewById(R.id.edit_verify_otp2);
TextView otp3 = (TextView) root.findViewById(R.id.edit_verify_otp3);
TextView otp4 = (TextView) root.findViewById(R.id.edit_verify_otp4);
/* Assign the TextViews in the array in the order in which you want to shift focus */
TextView[] otpTextViews = {otp1, otp2, otp3, otp4};
for (TextView currTextView : otpTextViews) {
currTextView.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
nextTextView().requestFocus();
}
@Override
public void afterTextChanged(Editable s) {
}
public TextView nextTextView() {
int i;
for (i = 0; i < otpTextViews.length - 1; i++) {
if (otpTextViews[i] == currTextView)
return otpTextViews[i + 1];
}
return otpTextViews[i];
}
});
}
于 2020-08-25T23:17:03.783 回答