如何使用TextWatcher检查给定的文本是否有效?

时间:2019-01-19 08:39:31

标签: android android-edittext android-textwatcher

我想在我的移动银行应用程序中实现MPIN功能。我正在使用文本观察程序来执行六个编辑文本。

一旦我完成了6个字段,如何自动检查该引脚是否正确以及如何显示警报。 如果Pin正确,则如何自动移动下一屏。

我使用此值将引脚存储在sharedpreference中,以检查条件。在哪里必须检查此条件。请帮助我。

这是我的代码:

public class AboutUsActivity extends AppCompatActivity {
EditText t1,t2,t3,t4,t5,t6;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_about_us);
    t1=(EditText) findViewById(R.id.first);
    t2=(EditText) findViewById(R.id.second);
    t3=(EditText) findViewById(R.id.third);
    t4=(EditText) findViewById(R.id.fourth);
    t5=(EditText) findViewById(R.id.fifth);
    t6=(EditText) findViewById(R.id.sixth);

    t1.addTextChangedListener(new GenericTextWatcher(t1));
    t2.addTextChangedListener(new GenericTextWatcher(t2));
    t3.addTextChangedListener(new GenericTextWatcher(t3));
    t4.addTextChangedListener(new GenericTextWatcher(t4));
    t5.addTextChangedListener(new GenericTextWatcher(t5));
    t6.addTextChangedListener(new GenericTextWatcher(t6));

 }
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

        Log.i("AA","aftertext--");
        String text = editable.toString();
        switch(view.getId())
        {

            case R.id.first:
                if(text.length()==1)
                    t2.requestFocus();
                if(text.length()==0)
                    t1.requestFocus();
                break;
            case R.id.second:
                if(text.length()==1)
                    t3.requestFocus();
                if(text.length()==0)
                    t1.requestFocus();
                break;
            case R.id.third:
                if(text.length()==1)
                    t4.requestFocus();
                if(text.length()==0)
                    t2.requestFocus();
                break;
            case R.id.fourth:
                if(text.length()==1)
                    t5.requestFocus();
                if(text.length()==0)
                    t3.requestFocus();
                break;
            case R.id.fifth:
                if(text.length()==1)
                    t6.requestFocus();
                if(text.length()==0)
                    t4.requestFocus();
                break;
            case R.id.sixth:
                if(text.length()==0)
                    t5.requestFocus();
                break;
        }
    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub
        Log.i("AA","beforetext--");
    }

    @Override
    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) 
{
        // TODO Auto-generated method stub
        Log.i("AA","textchanged--");
    }


}

}

1 个答案:

答案 0 :(得分:0)

 @Override
    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) 
    {
        if(arg0.length() == 6)
        {
           // method to check if code is correct or not
        }   

    }

或者简单的方法,使用库:

https://github.com/mukeshsolanki/android-otpview-pinview

这将在用户输入6位数字时给您回叫

private OtpView otpView;
 otpView = findViewById(R.id.otp_view);
 otpView.setListener(new OnOtpCompletionListener() {
   @Override public void onOtpCompleted(String otp) {

     // do Stuff
     Log.d("onOtpCompleted=>", otp);
   }
 });
相关问题