bakriawad
bakriawad

Reputation: 345

adding onClick listener in android and receiving call

It's easier to explain with code so here it is:

in Java, to add an action listener you do:

public class PROG implements ActionListener
{

   ......

   JButton a,b,c = new JButton("button");  //this is vague and not true, just for a point
   a,b,c.add(ActionListener(this);

   public void ActionPerforemed(ActionEvent e)
   {  //This bit V V 
      if(e.getSource == b) Sys.out.print("a");
      if(e.getSource == b) Sys.out.print("b");
      if(e.getSource == b) Sys.out.print("c");
   }

}

how can i use the al.getSource on android? what i have is:

public void onClick(View v) 
{
    if(v.?????? == a) Sys.out.print("a");
    if(v.?????? == b) Sys.out.print("b");
    if(v.?????? == c) Sys.out.print("c");
}

what is the method's name? and what is the View class anyway?

Upvotes: 0

Views: 135

Answers (1)

ucsunil
ucsunil

Reputation: 7494

To use the onClick() method, you have to make the Activity class implement the View.OnClickListener and implement the onClick() method.

The equivalent is:

public void onClick(View v) {
    if(v.getId() == R.id.idOfAButton) {showToastA();} // check the id of the button being clicked
    if(v.getId() == R.id.idOfBButton) {showToastB();}
    if(v.getId() == R.id.idOfCButton) {showToastC();}
}

Upvotes: 1

Related Questions