gabriele.pacor
gabriele.pacor

Reputation: 107

Same method repeated multiple times

I started to use Android Studio, and I'm doing a basic project. In this project there are 9 buttons that I control with the method setOnClickListener. I have to repet this method 9 times, is there another way to control them?

EDIT: This is what I've done so far:

 b1.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            if (b1.getText().toString().equals(""))
                if (turno == 1) {
                    turno = 2;
                    b1.setText("X");
                } else if (turno == 2) {
                    turno = 1;
                    b1.setText("O");
                }
            n_turni++;
            vincitore();
            b1.setEnabled(false);

        }
    });

    b2.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            if (b2.getText().toString().equals(""))
                if (turno == 1) {
                    turno = 2;
                    b2.setText("X");
                } else if (turno == 2) {
                    turno = 1;
                    b2.setText("O");
                }
            n_turni++;
            vincitore();
            b2.setEnabled(false);
        }


    });

    b3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (b3.getText().toString().equals(""))
                if (turno == 1) {
                    turno = 2;
                    b3.setText("X");
                } else if (turno == 2) {
                    turno = 1;
                    b3.setText("O");
                }
            n_turni++;
            vincitore();
            b3.setEnabled(false);
        }


    });

Upvotes: 0

Views: 587

Answers (4)

Amit kaushal
Amit kaushal

Reputation: 86

In XML

    <Button

android:id="@+id/button1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="TextButton"
/>

<Button

android:id="@+id/button2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="TextButton2"
/>

In Java File

Button b = (Button) findViewById(R.id.button1);
Button b2 = (Button) findViewById(R.id.button2);

b.setOnClickListener(this);
b2.setOnClickListener(this);

@Override

public void onClick(View v) {

int id = v.getId();

switch(id) {

case R.id.button1: 

// execeute our code


break;
case R.id.button2: 

// execeute our code


break;


}
}

No matter how many buttons you put in layout, but it is required you have to register each button for click listener once

According to android architecture you can overrride a single function .ie onClick() that will be called each time when click on button.You check which button was clicked by matching view ID.

Upvotes: 0

Grisgram
Grisgram

Reputation: 3253

The click method provides you already with a View parameter, which is the View (Button, in your case) that has been clicked.

So I'd suggest, you simply create your OnClickListener only once and assign it multiple times: The key here is the cast of v to Button. So it works for all your buttons.

View.OnClickListener listener = new View.OnClickListener() {
    public void onClick(View v) {
        Button b = (Button)v;
        if (b.getText().toString().equals(""))
            if (turno == 1) {
                turno = 2;
                b.setText("X");
            } else if (turno == 2) {
                turno = 1;
                b.setText("O");
            }
        n_turni++;
        vincitore();
        b.setEnabled(false);
    }
});

b1.setOnClickListener(listener);
b2.setOnClickListener(listener);    
b3.setOnClickListener(listener);
b4.setOnClickListener(listener);
b5.setOnClickListener(listener);    
b6.setOnClickListener(listener);
b7.setOnClickListener(listener);
b8.setOnClickListener(listener);    
b9.setOnClickListener(listener);

Upvotes: 2

Rajen Raiyarela
Rajen Raiyarela

Reputation: 5634

Create one common View.OnClickListener as

private View.OnClickListener buttonClickListener = new View.OnClickListener() {

   public void onClick(View v) {
       if (v.getId() = button1.getId()){// do button1 click function}         
       if (v.getId() = button2.getId()){// do button2 click function}         
   }

};
button1.setOnClickListener(buttonClickListener);
button2.setOnClickListener(buttonClickListener);

Assign buttonClickListener to all your buttons by setOnClickListener(buttonClickListener). To identify each button inside you onClick(View v) method compare v.getId() to your button.getId().

Same you can do by implementing View.OnClickListener to your class, which will ask you to override onClick method. So in your setOnClickListener simply pass this as your OnClickListener reference.

Upvotes: 0

M. le Rutte
M. le Rutte

Reputation: 3563

Add a parameter of type View to the method of the event handler. When called the this parameter will represent the view that was clicked. You can then either use its identity or content to determine which button it was and use the data.

See for the documentation of View.OnClickListener the Android documentation: https://developer.android.com/reference/android/view/View.OnClickListener.html

Edit: Now I've seen your code it is clearer to me what you mean. Solutions:

  1. Have your class implement View.OnClickListener and pass this as the parameter
  2. Extract the method into another method and call this one from each anonymous handler, the pre-lambda variant of 3.
  3. If your version of development supports lambdas pass a lamda function, which is a cleaner variant of 2.

Upvotes: 0

Related Questions