在每个字母分别位于不同位置的情况下编辑OTP的文本 [英] Edit text for OTP with Each letter in separate positions

查看:78
本文介绍了在每个字母分别位于不同位置的情况下编辑OTP的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,当用户想要重设密码时需要输入OTP密码,我需要一个文本,如所附图像中的文本...我想继续进行的是对每个字母分别使用单独的editText ,它们全部以水平方向的线性布局排列,边距和最大长度均为1,因此每个editText中只能输入一个字母...是正确的 Approach ?? 有任何建议吗?

I'm working on a application which asks for OTP when user want to reset his password for which I need a text like the one in attached Image... What I thought to proceed with is individual editText for each of the letter, All of them arranged in linear layout of horizontal orientation with some margin and max length as 1 so only one letter can be entered in each editText... Is that a right Approach?? Any Suggestions??

推荐答案

您可以尝试通过使 TextWatcher 更通用,使其易于使用和理解

You can try this, by making TextWatcher more Generic, so its easy to use and understand

使用以下课程:

public class GenericTextWatcher implements TextWatcher
    {
        private View view;
        private GenericTextWatcher(View view) 
        {
            this.view = view;
        }

        @Override
        public void afterTextChanged(Editable editable) {
            // TODO Auto-generated method stub
            String text = editable.toString();
            switch(view.getId())
            {

            case R.id.editText1:
                if(text.length()==1)
                    et2.requestFocus(); 
                break;
            case R.id.editText2:
                if(text.length()==1)
                    et3.requestFocus();
                else if(text.length()==0)
                    et1.requestFocus();  
                break;
            case R.id.editText3:
                if(text.length()==1)
                    et4.requestFocus();
                else if(text.length()==0)
                    et2.requestFocus();
                break;
            case R.id.editText4:
                if(text.length()==0)
                    et3.requestFocus();
                break;
            }
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            // TODO Auto-generated method stub
        }
    }

如何使用上述课程

et1.addTextChangedListener(new GenericTextWatcher(et1));
et2.addTextChangedListener(new GenericTextWatcher(et2));
et3.addTextChangedListener(new GenericTextWatcher(et3));
et4.addTextChangedListener(new GenericTextWatcher(et4));

这里 et1,et2,et3和et4 是您的EditText,我知道Java标准中的命名约定不好,但是您可以用它代替它.

Here et1,et2,et3 and et4 are your EditTexts, I know its bad naming convention as per Java Standard, but you can replace it with yours.

PS ,您可以在此处找到xml设计 GitHub其他一些示例设计xml供参考

P.S You can find the xml design for this here GitHub some other, sample design xml for reference

这篇关于在每个字母分别位于不同位置的情况下编辑OTP的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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