EventHandling


Last Updated:

  1. renegadeandy

    renegadeandy Member This Topic's Starter

    Joined:
    Jun 6, 2009
    Messages:
    16
    Likes Received:
    0
    Hey!

    Right i am starting to make my app and am happily moving through the tutorials on the android developer site.

    Gota question.

    Lets say I have an EditText box:

    Code (Text):
    1.  
    2. <EditText android:text="Insert Team Name"
    3.                      android:id="@+id/teamInput"
    4.                      android:layout_width="fill_parent"
    5.                      android:layout_height="wrap_content"
    6.                      android:layout_weight="1">
    7.                 </EditText>
    How do I get it to call some code once a user has finished inputting it - or is the idea that you have a search button, which retrieves the text on click - either way - can you please paste a bit of code, or a class, where I can create a new object be it a button or a EditText item where I can retrieve the contents to do something with!

    Cheers

    Andy
     

    Advertisement
  2. devolio

    devolio Well-Known Member

    Joined:
    Oct 30, 2008
    Messages:
    339
    Likes Received:
    37
    Assuming this bit of XML as your layout...

    Code (Text):
    1. <EditText android:text="Insert Team Name"
    2.                      android:id="@+id/teamInput"
    3.                      android:layout_width="fill_parent"
    4.                      android:layout_height="wrap_content"
    5.                      android:layout_weight="1">
    6.                 </EditText>
    Code (Text):
    1. EditText teamInput = (EditText) findViewById(R.id.teamInput);
    2. String tiVal = teamInput.getText().toString();
     
  3. renegadeandy

    renegadeandy Member This Topic's Starter

    Joined:
    Jun 6, 2009
    Messages:
    16
    Likes Received:
    0
    Wow ok - farely easy then - and what about a button - so how do i tie an xml button into java code?!
     
  4. devolio

    devolio Well-Known Member

    Joined:
    Oct 30, 2008
    Messages:
    339
    Likes Received:
    37
    This page has a decent sample of using UI elements. Handling UI Events | Android Developers

    Also, in your SDK, there's samples that have tons of good, fast code you can use to teach yourself as well.
     
  5. renegadeandy

    renegadeandy Member This Topic's Starter

    Joined:
    Jun 6, 2009
    Messages:
    16
    Likes Received:
    0
    Alright cool - trying to get something to work - do standard System.out.println("wowow"); debug statements work - because I am finding it hard to see where my code is dieing :

    Ok so I have this :

    Code (Text):
    1.  
    2.  OnClickListener searchTeamListener = new OnClickListener() {
    3.                    public void onClick(View v) {
    4.                      TextView txt = (TextView) v.findViewById(R.id.textview3);
    5.                      EditText search = (EditText) findViewById(R.id.searchTeam);
    6.                      System.out.println("woof");
    7.                      txt.setText(search.getText());
    8.                    }
    9.                };
    10.                
    11.                    // Capture our button from layout
    12.                    Button button = (Button)findViewById(R.id.searchTeam);
    13.                    // Register the onClick listener with the implementation above
    14.                    button.setOnClickListener(searchTeamListener);
    15.  
    However I cannot detect if it gets run - when I hit the button, it crashes the app on the emulator! AAAAAAAAAAAAAAAAA!:eek:
     
  6. GeraldNunn

    GeraldNunn Member

    Joined:
    Jun 6, 2009
    Messages:
    6
    Likes Received:
    1
    Use the static methods in the Log class to output debug statements, you can view them in the Eclipse LogCat view. Here is a very simple example:

    public class MyClass {
    private static final String "MyApp/MyClass";

    public void someMethod {
    Log.d(TAG, "This is a debugging statement");
    }
     
  7. cp123127

    cp123127 Member

    Joined:
    Jun 26, 2009
    Messages:
    6
    Likes Received:
    0
    I don't know if this will help but try putting
    Code (Text):
    1. TextView txt = (TextView) v.findViewById(R.id.textview3);
    2. EditText search = (EditText) findViewById(R.id.searchTeam);
    as method level fields and giving them the final modifier so that the onClick can grab them.
    And System.out.println won't work. Use Log.d instead.
     
  8. sarandasl

    sarandasl New Member

    Joined:
    Nov 3, 2009
    Messages:
    2
    Likes Received:
    0

Share This Page

Loading...