如何自动移动到android中的下一个编辑文本

时间:2012-09-18 03:39:29

标签: android

我在一行上有几个编辑文本框。在用户输入特定数量的字符后,我想自动移动到下一个编辑文本。我如何实现这一目标?

4 个答案:

答案 0 :(得分:60)

您可以使用Text Watcher类完成此操作,然后将焦点设置在TextWatcher的EditText方法中的下一个OnTextChanged()

在您的情况下,由于您有两个编辑文本,请说et1et2。您可以尝试以下代码: -

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
    }

});

我自己没有检查过代码,但我希望这可以帮助您解决问题。

答案 1 :(得分:14)

有一种更简单的方法可以做到这一点,它不涉及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) {}

答案 2 :(得分:2)

接受的答案很好。

如果您使用的是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);

答案 3 :(得分:0)

如果您有要转移焦点的TextViews列表(例如获取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];
            }
        });
    }