Knowing that a button was clicked [closed]

0

I would like to know that a certain Button has been clicked, so it does not pass null validation. Here is the code:

//OnClick da tela de registro
btnRegistrar = (Button)findViewById(R.id.email_registrar_button);
btnRegistrar.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        intent = new Intent(LoginActivity.this,RegistrarActivity.class);
        //startNewIntent("registrar");
    }
});

But before opening the activity it passes in this validation:

if ((TextUtils.isEmpty(email) || TextUtils.isEmpty(password) && (!btnRegistrar.isPressed()))) {
    mEmailView.setError(getString(R.string.error_field_required));
    mPasswordView.setError(getString(R.string.error_field_required));
    focusView = mEmailView;
    cancel = true;

This "isPressed ()" was one of the tentives. Thanks!

Edit: When clicking the button it calls this validation, I used the android studio login template

Edti2: Resolved the question, it was coding error. I can not delete the question.

    
asked by anonymous 08.06.2016 / 21:36

3 answers

2

Speak Henry,

You can use the Boolean property:

You declare a variable of type boolean, named clicked and the default value as false ;

private boolean clicked = false;

Then, within the click of the button, you change this variable to true , like this:

btnRegistrar.setOnClickListener(new OnClickListener(){
   @Override
   public void onClick(View v) {

      clicked = true;

      if(clicked) {
          if ((TextUtils.isEmpty(email) || TextUtils.isEmpty(password))){
              mEmailView.setError(getString(R.string.error_field_required));
              mPasswordView.setError(getString(R.string.error_field_required));
              focusView = mEmailView;
          }else{
              intent = new Intent(LoginActivity.this,RegistrarActivity.class);
              startActivity(intent);
          }
      }
   }
});

Hugs.

    
08.06.2016 / 21:49
0

Maybe you have misunderstood, but from what I understand by clicking on btnRegistrar you will check if the fields are not empty, correct?

It seems that the validation will be inside the OnClickListener event so this code snippet will only run if the button is pressed, there is no need to check if it was clicked. If you are going to check if another button has been clicked, you should use a variable to store this information when the other event is triggered.

If the reason for your check is to prevent the button from being null, this check should be done when you assign the value to the variable, because when it does not find the view it can trigger a NullPointerException.

And lastly if you want to store a configuration more permanently, consider changing the button by a switch or something, since the botao.isPressed () will only return true if the user is still squeezing it and not gone pressed.

Any questions I am at your disposal :)

    
08.06.2016 / 21:55
0

I would declare the EditText as global and in the click event I would do this:

btnRegistrar = (Button)findViewById(R.id.email_registrar_button);
btnRegistrar.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
    if(critica){
     intent = new Intent(LoginActivity.this,RegistrarActivity.class);
    //startNewIntent("registrar");
    }     
}});


private boolean critica() {
    if (mEmailView.getText().toString().isEmpty()) {
        mEmailView.setError(getString(R.string.error_field_required));
        return false;
    }

    if (mPasswordView.getText().toString().isEmpty()) {
        mPasswordView.setError(getString(R.string.error_field_required));
        return false;
    }

    return true;
}
    
10.06.2016 / 14:56