如何将焦点更改为android中的下一个编辑文本?

时间:2023-03-28
本文介绍了如何将焦点更改为android中的下一个编辑文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户只能在编辑文本中输入一位数字.如果他在 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

如果你想修改你的密码,那么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();
                 }

             }
         });

希望这项工作.

这篇关于如何将焦点更改为android中的下一个编辑文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:更改浮动标签 EditText 和 TextInputLayout 的字体 下一篇:Android以编程方式禁用模拟器中EditText的自动完成

相关文章

最新文章