如何将焦点移到下一个编辑文本的Andr​​oid? [英] How to change the focus to next edit text in android?

查看:105
本文介绍了如何将焦点移到下一个编辑文本的Andr​​oid?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户可以只输入一个在编辑文本的数字。如果他进edtText1的价值,我想光标会自动移动到edtText2等。用户可以能够编辑,他/她已经输入的文本。我尝试以下方法。

The User can enter only one digit in the edit text. if he enters the value in edtText1, I want the cursor automatically moves to edtText2 and so on. The user can able to edit the text which he/she has entered already. I tried the following way.

    edtPasscode1.setOnKeyListener(new OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // TODO Auto-generated method stub
            if (edtPasscode1.getText().length() == 1)
                edtPasscode2.requestFocus();
            return false;
        }
    });

    edtPasscode2.setOnKeyListener(new OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // TODO Auto-generated method stub
            if (edtPasscode2.getText().length() == 1)
                edtPasscode3.requestFocus();
            return false;
        }
    });

    edtPasscode3.setOnKeyListener(new OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // TODO Auto-generated method stub
            if (edtPasscode3.getText().length() == 1)
                edtPasscode4.requestFocus();
            return false;
        }
    });

如果用户编辑文本,将光标移动到其他一些editTexts并没有发挥期望。我怎样才能实现以上?

If the user edit the text, The cursor moves to some other editTexts and not working as desired. How can i achieve the above?

推荐答案

尝试 TextWatcher 而不是 onKeyListener

B'coz如果要修改密码,在这种情况下的 TextWatcher 会给你更多的方法来处理。

B'coz if want to edit your password, in that case TextWatcher will give you more method to dealt with..

编辑: -

StringBuilder sb=new StringBuilder();

         edtPasscode1.addTextChangedListener(new TextWatcher() {
             public void onTextChanged(CharSequence s, int start, int before, int count) {
                 // TODO Auto-generated method stub
                 if(sb.length()==0&edtPasscode1.length()==1)
                 {
                     sb.append(s);
                     edtPasscode1.clearFocus();
                     edtPasscode2.requestFocus();
                     edtPasscode2.setCursorVisible(true);

                 }
             }

             public void beforeTextChanged(CharSequence s, int start, int count,
                     int after) {

                 if(sb.length()==1)
                 {

                     sb.deleteCharAt(0);

                 }

             }

             public void afterTextChanged(Editable s) {
                 if(sb.length()==0)
                 {

                     edtPasscode1.requestFocus();
                 }

             }
         });

希望这项工作。

Hope this work.

这篇关于如何将焦点移到下一个编辑文本的Andr​​oid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆