如何使用文本观察器检查给定的文本是否有效



我想在我的手机银行应用程序中实现MPIN功能。我有六个使用文本观察器的编辑文本,我正在执行此功能。

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

我使用此值将引脚存储在共享首选项中如何检查条件。我必须检查此条件的地方。请帮助我。

这是我的代码:

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--");
    }

}
}
 @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);
   }
 });

相关内容

  • 没有找到相关文章

最新更新