Does the Listener know the name of the klicked button?

Put your problem here if it does not fit any of the other categories.

Does the Listener know the name of the klicked button?

Postby thedeftone » Wed Apr 09, 2008 8:21 pm

Hello Forum :)

Is there a method with which you can call the id/name of the button in an onClicklistener form the button which was clicked?

Let's say I have this sort of Layout-XML with 2 Buttons:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <AbsoluteLayout
  5.  
  6. android:id="@+id/widget33"
  7.  
  8. android:layout_width="fill_parent"
  9.  
  10. android:layout_height="fill_parent"
  11.  
  12. xmlns:android="http://schemas.android.com/apk/res/android"
  13.  
  14. >
  15.  
  16. <Button
  17.  
  18. android:id="@+id/nameClick"
  19.  
  20. android:layout_width="86px"
  21.  
  22. android:layout_height="wrap_content"
  23.  
  24. android:text="Klick"
  25.  
  26. android:layout_x="0px"
  27.  
  28. android:layout_y="51px"
  29.  
  30. >
  31.  
  32. </Button>
  33.  
  34. <Button
  35.  
  36. android:id="@+id/blahClick"
  37.  
  38. android:layout_width="86px"
  39.  
  40. android:layout_height="wrap_content"
  41.  
  42. android:text="Klick"
  43.  
  44. android:layout_x="0px"
  45.  
  46. android:layout_y="100px"
  47.  
  48. >
  49.  
  50. </Button>
  51.  
  52. </AbsoluteLayout>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


And a class like that:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class MaitDoKu extends Activity {
  3.  
  4.        
  5.  
  6.     private Button nameClick, blahClick;
  7.  
  8.        
  9.  
  10.     /** Called when the activity is first created. */
  11.  
  12.     @Override
  13.  
  14.     public void onCreate(Bundle icicle) {
  15.  
  16.         super.onCreate(icicle);
  17.  
  18.         setContentView(R.layout.eingabe);
  19.  
  20.         addListener();
  21.  
  22.     }
  23.  
  24.    
  25.  
  26.     public void addListener(){
  27.  
  28.         nameClick = (Button) findViewById(R.id.nameClick);
  29.  
  30.         blahClick = (Button) findViewById(R.id.blahClick);
  31.  
  32.         nameClick.setOnClickListener(allButtonListener);
  33.  
  34.         blahClick.setOnClickListener(allButtonListener);
  35.  
  36.     }
  37.  
  38.  
  39.  
  40.     OnClickListener allButtonListener = new OnClickListener() {
  41.  
  42.         public void onClick(View v) {
  43.  
  44.             finish();
  45.  
  46.         }
  47.  
  48.     };
  49.  
  50. }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


Is there any chance that the Listener knows which of the Buttons was klicked?

Greetz
thedeftone
Junior Developer
Junior Developer
 
Posts: 14
Joined: Wed Apr 09, 2008 7:51 pm

Top

Postby cadlg » Wed Apr 09, 2008 8:47 pm

Hi thedeftone.

The View parameter of the onClick method identifies the button that was pressed.

public void onClick(View v) {
finish();
}

Hope this helps...


cadlg
cadlg
Experienced Developer
Experienced Developer
 
Posts: 84
Joined: Wed Feb 20, 2008 12:33 am
Location: Guatemala

Postby thedeftone » Wed Apr 09, 2008 9:08 pm

Hi cadlg

that helped, yes :)
And the solution almost was there jumping on my face ;)

But now I wonder if there's a way to get the text of the Button.
From that view V i only get the Id which is in a quite (for me :D) cryptic format.

For my (in future) project I'll have to change a Button's Text by clicking on it. I just can't figure out how to do this. :?
thedeftone
Junior Developer
Junior Developer
 
Posts: 14
Joined: Wed Apr 09, 2008 7:51 pm

Postby cadlg » Wed Apr 09, 2008 9:15 pm

You need to cast the view to your Button type.

Something like this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Button temp = (Button)view;
  2.  
  3. String text = temp.getText().toString();
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Regards,


cadlg
cadlg
Experienced Developer
Experienced Developer
 
Posts: 84
Joined: Wed Feb 20, 2008 12:33 am
Location: Guatemala

Postby thedeftone » Wed Apr 09, 2008 9:20 pm

That is exactly what I meant! :)

You saved my day!! :lol:

I didn't know that it's just as easy as a typecast...
thedeftone
Junior Developer
Junior Developer
 
Posts: 14
Joined: Wed Apr 09, 2008 7:51 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 13 guests