other way is what jaswant has suggested, but i wouldnt use it..... not
object oriented...i persoanlly dont like functions which has  too many ifs
then elses, in your case it may go upto 100
also if your requirment is just changing a label...then i would say do
somethin glike this


Class MyButton extends Button implemnts ClickListener{
TextView textView;

public MyButton (){
this.textView = tv;
}

public MyButton (TextView tv){
this.textView = tv;
}

public void onButtonClick(View view){
  tv.setText("Whatever you want to change");
}
}

Now inject your textView either by Constructor or create a setter and use
that. Now you will write only one ClickListener and all will work same way.

Use this if all buttons have same functionality. And this is quite basic
pattern you can modify it as per your other requirments.



On Tue, Jul 5, 2011 at 11:11 AM, Rockline <[email protected]> wrote:

> Hi,
>
> Thanks for your answer.
> However, this implies that I'll have to declare setOnClickListener for
> each button in my Activity object (in the constructor):
>        Button MyButton1 = (Button)findViewById(R.id.MyButton1);
>        generate_button.setOnClickListener(new
> Button.OnClickListener() {
>            public void onClick(View v) {
>                updateLabel();
>            }
>        });
>        Button MyButton2 = (Button)findViewById(R.id.MyButton2);
>        generate_button.setOnClickListener(new
> Button.OnClickListener() {
>            public void onClick(View v) {
>                updateLabel();
>            }
>        });
>        Button MyButton3 = (Button)findViewById(R.id.MyButton3);
>        generate_button.setOnClickListener(new
> Button.OnClickListener() {
>            public void onClick(View v) {
>                updateLabel();
>            }
>        });
>
>        ....
>
>
>
> If I've got 100 buttons, it's gonna take me a while.
> Is there any other way to do it?
>
>
>
>
> On Jul 5, 11:01 am, Jaswant <[email protected]> wrote:
> > It's simple:
> > call setonClickListener() method for each button u'hv created.
> > and in onClick() method u can find clicked button by checking its text
> >
> > eg:
> > void onClick(View v)
> > {
> >    MyCustButton  btn=(MyCustButton )v;
> >     if(btn.getText().toString().equals("blabla"))
> >       ....................
> >
> >
> >
> >
> >
> >
> >
> > }
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to