Reuse code on multi-button onClick

Asked

Viewed 412 times

2

I’m developing an app where it will contain several buttons.

I need your help to see if I can improve the code.

Actions do the same thing just change the background color and pass me the button value.

The application has several other screens that have many buttons with almost the same actions, only with some changes in return.

(Imagem) Estou usando layout linear

My code:

btnBall01.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    count1++;
                    selectedNumber++;
                    games.setBallId(btnBall01.getId());
                    btnBall01.setBackgroundResource(R.mipmap.ball_verde);
                    games.setActualNumber(btnBall01.getText().toString());
                    Toast.makeText(ActivityMegasenaCreateGame.this, "Verde " + games.getActualNumber(), Toast.LENGTH_SHORT).show();

                    if (count1 != 1){
                        count1 = 0;
                        selectedNumber = selectedNumber -2;
                        btnBall01.setBackgroundResource(R.mipmap.ball);
                         Toast.makeText(ActivityMegasenaCreateGame.this, "Branco " + games.getActualNumber(), Toast.LENGTH_SHORT).show();
                        initializeComponentsBallsButtons();
                    }
                }
            });

            btnBall02.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    btnBall02.setBackgroundResource(R.mipmap.ball_verde);
                    count2++;
                    selectedNumber++;
                    games.setBallId(btnBall02.getId());
                    games.setActualNumber(btnBall02.getText().toString());

                    if (count2 != 1){
                        count2 = 0;
                        selectedNumber = selectedNumber -2;
                        btnBall02.setBackgroundResource(R.mipmap.ball);
                        initializeComponentsBallsButtons();
                    }
                }
            });

            btnBall03.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    btnBall03.setBackgroundResource(R.mipmap.ball_verde);
                    count3++;
                    selectedNumber++;
                    games.setBallId(btnBall03.getId());
                    games.setActualNumber(btnBall03.getText().toString());

                    if (count3 !=1){
                        count3 = 0;
                        selectedNumber = selectedNumber -2;
                        btnBall03.setBackgroundResource(R.mipmap.ball);
                        initializeComponentsBallsButtons();
                    }
                }
            });

            btnBall04.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    btnBall04.setBackgroundResource(R.mipmap.ball_verde);
                    count4++;
                    selectedNumber++;
                    games.setBallId(btnBall04.getId());
                    games.setActualNumber(btnBall04.getText().toString());

                    if (count4 != 1){
                        count4 = 0;
                        selectedNumber = selectedNumber -2;
                        btnBall04.setBackgroundResource(R.mipmap.ball);
                        initializeComponentsBallsButtons();
                    }

                }
            });

            btnBall05.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    btnBall05.setBackgroundResource(R.mipmap.ball_verde);
                    count5++;
                    selectedNumber++;
                    games.setBallId(btnBall05.getId());
                    games.setActualNumber(btnBall05.getText().toString());

                    if (count5 != 1){
                        count5 = 0;
                        selectedNumber = selectedNumber -2;
                        btnBall05.setBackgroundResource(R.mipmap.ball);
                        initializeComponentsBallsButtons();
                    }
                }
            });

            btnBall06.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    btnBall06.setBackgroundResource(R.mipmap.ball_verde);
                    count6++;
                    selectedNumber++;
                    games.setBallId(btnBall06.getId());
                    games.setActualNumber(btnBall06.getText().toString());

                    if (count6 != 1){
                        count6 = 0;
                        selectedNumber = selectedNumber -2;
                        btnBall06.setBackgroundResource(R.mipmap.ball);
                        initializeComponentsBallsButtons();
                    }
                }
            });

            btnBall07.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    btnBall07.setBackgroundResource(R.mipmap.ball_verde);
                    count7++;
                    selectedNumber++;
                    games.setBallId(btnBall07.getId());
                    games.setActualNumber(btnBall07.getText().toString());

                    if (count7 != 1){
                        count7 = 0;
                        selectedNumber = selectedNumber -2;
                        btnBall07.setBackgroundResource(R.mipmap.ball);
                        initializeComponentsBallsButtons();
                    }
                }
            });

            btnBall08.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    btnBall08.setBackgroundResource(R.mipmap.ball_verde);
                    count8++;
                    selectedNumber++;
                    games.setBallId(btnBall08.getId());
                    games.setActualNumber(btnBall08.getText().toString());

                    if (count8 != 1){
                        count8 = 0;
                        selectedNumber = selectedNumber -2;
                        btnBall08.setBackgroundResource(R.mipmap.ball);
                        initializeComponentsBallsButtons();
                    }
                }
            });

            btnBall09.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    btnBall09.setBackgroundResource(R.mipmap.ball_verde);
                    count9++;
                    selectedNumber++;
                    games.setBallId(btnBall09.getId());
                    games.setActualNumber(btnBall09.getText().toString());

                    if (count9 != 1){
                        count9 = 0;
                        selectedNumber = selectedNumber -2;
                        btnBall09.setBackgroundResource(R.mipmap.ball);
                        initializeComponentsBallsButtons();
                    }
                }
            });

            btnBall10.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    btnBall10.setBackgroundResource(R.mipmap.ball_verde);
                    count10++;
                    selectedNumber++;
                    games.setBallId(btnBall10.getId());
                    games.setActualNumber(btnBall10.getText().toString());

                    if (count10 != 1){
                        count10 = 0;
                        selectedNumber = selectedNumber -2;
                        btnBall10.setBackgroundResource(R.mipmap.ball);
                        initializeComponentsBallsButtons();
                    }
                }
            });

            btnBall11.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    btnBall11.setBackgroundResource(R.mipmap.ball_verde);
                    count11++;
                    selectedNumber++;
                    games.setBallId(btnBall11.getId());
                    games.setActualNumber(btnBall11.getText().toString());

                    if (count11 != 1){
                        count11 = 0;
                        selectedNumber = selectedNumber -2;
                        btnBall11.setBackgroundResource(R.mipmap.ball);
                        initializeComponentsBallsButtons();
                    }
                }
            });

            btnBall12.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    btnBall12.setBackgroundResource(R.mipmap.ball_verde);
                    count12++;
                    selectedNumber++;
                    games.setBallId(btnBall12.getId());
                    games.setActualNumber(btnBall12.getText().toString());

                    if(count12 != 1){
                        count12 = 0;
                        selectedNumber = selectedNumber -2;
                        btnBall12.setBackgroundResource(R.mipmap.ball);
                        initializeComponentsBallsButtons();
                    }
                }
            });

            btnBall13.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    btnBall13.setBackgroundResource(R.mipmap.ball_verde);
                    count13++;
                    selectedNumber++;
                    games.setBallId(btnBall13.getId());
                    games.setActualNumber(btnBall13.getText().toString());

                    if(count13 != 1){
                        count13 = 0;
                        selectedNumber = selectedNumber -2;
                        btnBall13.setBackgroundResource(R.mipmap.ball);
                        initializeComponentsBallsButtons();
                    }
                }
            });

            btnBall14.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    btnBall14.setBackgroundResource(R.mipmap.ball_verde);
                    count14++;
                    selectedNumber++;
                    games.setBallId(btnBall14.getId());
                    games.setActualNumber(btnBall14.getText().toString());

                    if (count14 != 1){
                        count14 = 0;
                        selectedNumber = selectedNumber -2;
                        btnBall14.setBackgroundResource(R.mipmap.ball);
                        initializeComponentsBallsButtons();
                    }
                }
            });

2 answers

3


I knew that:

  1. the same method can be associated with the multi-button click event?
  2. in an array different values can be saved, which can be accessed through an index?
  3. an object in the View class can be stored a reference to any object?

Now that you know, it’s easy to solve the problem.

(1) allows having only one method to handle the click of all buttons.
(2) allows storing all counters in one variable.
(3) allows storing the position in the array where the value of the counter corresponding to that button is stored.

The method must have a signature of the type:

public void nomeMetodo(View button) 

The method is assigned to each click event using the attribute android:onClick, in the declaration of each button in XML:

android:onClick="nomeMetodo"

The array must be of type int and have the number of items equal to the number of buttons.

In the declaration of each button, in XML, use the attribute android:tag to store the position in the array of your counter.

android:tag="0"

Use 0 for button 1, 1 for button 2 and so on.

In java, within the method nomeMetodo, this value can be obtained with

int tag = (int)button.getTag();

Use it in the array to get the counter value.

Supplementary information:

  • Thank you very much, it worked!!

0

You can create a method, with all this repetitive code, and each time you need it just call it, and as you want a return of a number you just set the type of method as int or double and make the return at the end, and put as parameters what you need within the method, maybe you have to think a little to create a "universal method" for all your buttons

public int meuMetodo(int parametro){
  int numero=2;
  int resultado=numero+parametro;
  return resultado;
}

I made just one example because I don’t understand what you really want in return, but I hope it helped you

Browser other questions tagged

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