开发者

Validation in android

I need allow the user to Enter only number and period in android through code

please help me

    InputFilter filter = new InputFilter() {      
        public CharSequence filter(CharSequence source, int start, int end,  Spanned dest, int dstart, int dend) {

            System.out.println("source " + source);
            System.out.println("dSta " + dstart);
            System.out.println("dEnd " + dend);
            System.out.println("Start " + start);
            System.out.println("End "开发者_如何学Go + end);

             for (int i = 0; i < source.length(); i++) {     
                 if (!Character.isDigit(source.charAt(i))) {
                        System.out.println("Character.isDigit(source.charAt(i))  " + Character.isDigit(source.charAt(i)));
                     return "";               
                     }
                /* if (!Character.isDefined('.')) {          
                     return "";               
                     } */      
                 }
             return null;

            }
        }; 


simply set the inputType to your View which you need to validate.For example for numbers

yourview.setInputType(InputType.TYPE_CLASS_NUMBER);

for decimal numbers

yourview.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);


Here is a whole code for validation...

enter code herepublic class LoginActivity extends AppCompatActivity {
private static final String TAG = "LoginActivity";
private static final int REQUEST_SIGNUP = 0;

@Bind(R.id.input_email) EditText _emailText;
@Bind(R.id.input_password) EditText _passwordText;
@Bind(R.id.btn_login) Button _loginButton;
@Bind(R.id.link_signup) TextView _signupLink;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    ButterKnife.bind(this);

    _loginButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            login();
        }
    });

    _signupLink.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // Start the Signup activity
            Intent intent = new Intent(getApplicationContext(), SignupActivity.class);
            startActivityForResult(intent, REQUEST_SIGNUP);
            finish();
            overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
        }
    });
}

public void login() {
    Log.d(TAG, "Login");

    if (!validate()) {
        onLoginFailed();
        return;
    }

    _loginButton.setEnabled(false);

    final ProgressDialog progressDialog = new ProgressDialog(LoginActivity.this,
            R.style.AppTheme_Dark_Dialog);
    progressDialog.setIndeterminate(true);
    progressDialog.setMessage("Authenticating...");
    progressDialog.show();

    String email = _emailText.getText().toString();
    String password = _passwordText.getText().toString();

    // TODO: Implement your own authentication logic here.

    new android.os.Handler().postDelayed(
            new Runnable() {
                public void run() {
                    // On complete call either onLoginSuccess or onLoginFailed
                    onLoginSuccess();
                    // onLoginFailed();
                    progressDialog.dismiss();
                }
            }, 3000);
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_SIGNUP) {
        if (resultCode == RESULT_OK) {

            // TODO: Implement successful signup logic here
            // By default we just finish the Activity and log them in automatically
            this.finish();
        }
    }
}

@Override
public void onBackPressed() {
    // Disable going back to the MainActivity
    moveTaskToBack(true);
}

public void onLoginSuccess() {
    _loginButton.setEnabled(true);
    finish();
}

public void onLoginFailed() {
    Toast.makeText(getBaseContext(), "Login failed", Toast.LENGTH_LONG).show();

    _loginButton.setEnabled(true);
}

public boolean validate() {
    boolean valid = true;

    String email = _emailText.getText().toString();
    String password = _passwordText.getText().toString();

    if (email.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
        _emailText.setError("enter a valid email address");
        valid = false;
    } else {
        _emailText.setError(null);
    }

    if (password.isEmpty() || password.length() < 4 || password.length() > 10) {
        _passwordText.setError("between 4 and 10 alphanumeric characters");
        valid = false;
    } else {
        _passwordText.setError(null);
    }

    return valid;
}

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜