(Java, Android) Button - Set Click’s limit

Asked

Viewed 29 times

0

Good, can someone tell me how to set a limit to the number of clicks on a button, and when you reach that limit, prevent the button from being clicked again?

Here it is:

 public void N1(View v) {
    EditText editTextView = (EditText) findViewById(R.id.editTextView);
    TextView tvN1 = (TextView) findViewById(R.id.textViewNumero1);
    editTextView.append(tvN1.getText().toString());}

This button works like a keyboard key, when clicked it shows the character in editText. I wanted it to be mandatory to press the button first before going through the next phase, but to prevent the button from being clicked more than once...

Thank you if you can help me

2 answers

3


Yes, just use the setEnabled(false property);

final Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        btn.setEnabled(false);
    }
});
  • Thank you very much, but by the way, you know how I can count the times you click a button?

  • 2

    @Eduardobrito using a variable that increments within the code of click and not forgetting to keep and restore the state of Activity

2

This can help you

int quantidade_clicks = 0;
int vezes_click = 5;

public void N1(View v) {
    if(this.quantidade_clicks<=this.vezes_click){
        EditText editTextView = (EditText) findViewById(R.id.editTextView);
        TextView tvN1 = (TextView) findViewById(R.id.textViewNumero1);
        editTextView.append(tvN1.getText().toString());
        this.quantidade_clicks++;
        return;
    }
    Toast.makeText(this,"você não pode clicar mais de "+String.valueOf(this.vezes_click)+ "vezes!",10).show();
}
  • I would take the findViewById() from within the method.

  • @ramaral it should ta affecting another view of the same layout of the button by clicking it

Browser other questions tagged

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