0

My app currently has 5 buttons (I'm going to add more later) and when each button is clicked, it'll assign a number to an item.

I'm wondering if there's a more efficient way of writing the setOnClickListner (it seems like I have to use that since I'm using this as a fragment. I found a way to do it if I was to assign an onClick in the xml but I can't apply that to this part of the code). I have it written out 5 times (and in the future it'll be more)

    buttons[0] = (Button) view.findViewById(R.id.cut1Btn);
    buttons[1] = (Button) view.findViewById(R.id.cut2Btn);
    buttons[2] = (Button) view.findViewById(R.id.cut3Btn);
    buttons[3] = (Button) view.findViewById(R.id.cut4Btn);
    buttons[4] = (Button) view.findViewById(R.id.cut5Btn);

    buttons[0].setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getActivity(), "TESTING BUTTON CLICK 1",Toast.LENGTH_SHORT).show();
            data = 1;
        }
    });

    buttons[1].setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getActivity(), "TESTING BUTTON CLICK 2",Toast.LENGTH_SHORT).show();
            data = 2;
        }
    });

    buttons[2].setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getActivity(), "TESTING BUTTON CLICK 3",Toast.LENGTH_SHORT).show();
            data = 3;
        }
    });

    buttons[3].setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getActivity(), "TESTING BUTTON CLICK 4",Toast.LENGTH_SHORT).show();
            data = 4;
        }
    });

    buttons[4].setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getActivity(), "TESTING BUTTON CLICK 5",Toast.LENGTH_SHORT).show();
            data = 5;
        }
    });

Could I maybe do a switch or a loop? Like assign i = 0, i < 5, i++ for the button array and then for data make that data = i + 1 ? If so, any suggestions on how I can do that?

Thanks!

Pam
  • 399
  • 2
  • 16

3 Answers3

2

THe other answers here will work, but either use the tag (generally a bad idea and prevents any other use) and aren't really object oriented. Instead you should make a class an instantiate it.

private class MyClickListener {
  private int data;
  public MyClickListener(int data) {
     this.data = data;
  } 
  public void onClick(View view) {
      Toast.makeText(getActivity(), "TESTING BUTTON CLICK" + data,Toast.LENGTH_SHORT).show();
  }
}

...

int i=0;
for(Button button : buttons) {
  button.setOnClickListener(new MyClickListener(i++));
}
Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
0

You could use a common method and then implement a switch case based upon R.id of your button An example would in like this. in onCreate(--) method

cut1Btn = (Button) view.findViewById(R.id.cut1Btn);
    cut2Btn = (Button) view.findViewById(R.id.cut2Btn);
    cut3Btn = (Button) view.findViewById(R.id.cut3Btn);
    cut4Btn = (Button) view.findViewById(R.id.cut4Btn);
    cut5Btn = (Button) view.findViewById(R.id.cut5Btn);

cut1Btn.setOnClickListener(this);
cut2Btn.setOnClickListener(this);
cut3Btn.setOnClickListener(this);
cut4Btn.setOnClickListener(this);
cut5Btn.setOnClickListener(this);

Implement View.onClickListener in your activity and override this method in your activity

public void onClick(View v){
switch(v.getId()){
case R.id.cut1Btn:
//Put Your Code Here
break;
case R.id.cut2Btn:
//Put Your Code Here
break; 
case R.id.cut3Btn:
//Put Your Code Here
break;
case R.id.cut4Btn:
//Put Your Code Here
break;
case R.id.cut5Btn:
//Put Your Code Here
break;
}
}
Sahil Manchanda
  • 9,812
  • 4
  • 39
  • 89
  • This looks great, thanks! Question though, what do you mean by "Implement View.onClickListener in your activity and override this method in your activity". How do I do that? – Pam Jul 11 '17 at 03:58
  • @D.Kate look at this https://stackoverflow.com/a/29479937/2299040 answer's last part – Sahil Manchanda Jul 11 '17 at 04:03
  • @D.Kate don't mind buddy . But i thing you should rebrush with core part of java. Concrete class, Abstract class and interfaces – ADM Jul 11 '17 at 04:23
0

You can implement a switch to do this things:

  1. Your class must implement OnClickListener and override function OnClick(View view).

  2. Then just set OnClickListener(this) like this on onCreate method

    buttons[1].setOnClickListener(this)

    buttons[2].setOnClickListener(this)

    buttons[3].setOnClickListener(this)

  3. do a switch-case on override function

    @Override

    public void onClick(View v) {

    switch (v.getId()) {
        case R.id.button1:
            // button 1 do
            break;
        case R.id.button2:
            // button 2 do
            break;
        case R.id.button3:
            // button 3 do
            break;
    }
    

    }

Khang Tran
  • 467
  • 5
  • 16