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

How to change the focus to next edit text in android?(如何将焦点更改为android中的下一个编辑文本?)
本文介绍了如何将焦点更改为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中的下一个编辑文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)