oopsi and forgot to write...in constructor make sure you assign that
fucntion as setOnCLickListener

this.setOnCLickListener(this)

public MyButton (){
this.setOnCLickListener(this)
}

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

On Tue, Jul 5, 2011 at 11:20 AM, Ravi Sharma <ping2r...@gmail.com> wrote:

> 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 (){
>
> }
>
> 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 <rockl...@gmail.com> 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 <jaswant1...@gmail.com> 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 android-developers@googlegroups.com
>> To unsubscribe from this group, send email to
>> android-developers+unsubscr...@googlegroups.com
>> 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 android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to