Knowing that a button has been clicked

Asked

Viewed 2,658 times

0

I would like to know that a certain Button has been clicked, so that it does not pass the null validation. Follows 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 Activity he passes 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 trials. Thank you!

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

Edti2: Rewritten the question, it was an encoding error. I cannot delete the question.

  • You will only register if the button is not pressed ?

  • Ever thought about using a global variable as a flag? or don’t like doing gamibarra hehe

  • Oops, maybe the description was incomplete, I tried to use the flag, but even putting the onclick, it passes first in this validation, is out of onclick. I used the login time of own android studio

  • @Lucasqueirozribeiro The registration screen will open only if the button is pressed

  • Answer as you solved and mark as closed, or choose one of the answers

  • @Lucasqueirozribeiro I don’t know how to close, or doesn’t appear to me

Show 1 more comment

3 answers

2

Fala Henrique,

You can use the property Boolean:

You declare a variable of type Boolean, with the name clicked and the default value as false;

private boolean clicked = false;

Then, inside the button click, you change this variable to true, thus:

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.

  • Oops, maybe the description was incomplete, I tried to use the flag, but even putting the onclick, it passes first in this validation, is out of onclick. I used the login time of own android studio

0

You may have misunderstood, however, as I understood by clicking on btnRegister you will check that the fields are not empty correct ?

It seems to me that the validation will be inside the Onclicklistener event so this code snippet will only be executed 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 not finding the view can trigger a Nullpointerexception.

And lastly if you want to store a more permanent setting, consider swapping the button for a switch or something like that, because the.isPressed() button will only return true if the user is still pressing it and has not been pressed.

Any doubt I’m available :)

  • Oops, maybe the description was incomplete, I tried to use the flag, but even putting the onclick, it passes first in this validation, is out of onclick. I used the login time of own android studio

  • Oh yes, well, I imagine that this check is within one onCreate() or onStart() correct ? If this is the case, this check will only be done when starting Activity, ideally you call this check after the button is clicked, because this way the user will probably have already changed the fields. I believe there are two simple ways to solve this, one is to put this check inside the event, and you involve your input into one if(!cancel) or within your click event you call another method containing this check and returning the Cancel.

0

I would declare Edittext as global and in the click event I would do so:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.