首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在android中自动移动到下一个编辑文本

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

Stack Overflow用户
提问于 2012-09-18 11:39:29
回答 4查看 38.2K关注 0票数 38

我在一行上有几个编辑文本框。当用户在第一个文本中键入特定数量的字符后,我希望自动移动到下一个编辑文本。我该如何实现这一点?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-09-18 12:20:33

可以通过使用Text Watcher类来实现这一点,然后在EditTextOnTextChanged()方法中将焦点设置在下一个TextWatcher上。

在您的例子中,因为您有两个编辑文本,比如et1et2。你可以试试下面的代码:

代码语言:javascript
复制
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
    }

});

我自己还没有签出代码,但我希望这能帮助你解决你的问题。

票数 74
EN

Stack Overflow用户

发布于 2017-01-30 02:46:02

有一种更简单的方法可以做到这一点,它不需要知道EditText视图的is。用于android:maxLength="1“。

代码语言:javascript
复制
// 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) {}
票数 23
EN

Stack Overflow用户

发布于 2017-04-20 11:29:24

公认的答案是好的。

如果你正在使用RxJava + RxBinding库,我提供了新的方法。

编码表明需要验证4个输入编辑文本(我的案例是通过电话-电话验证的激活代码SMS )

代码语言:javascript
复制
// 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);
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12470067

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档