Doughy wrote:
> Is
> it necessary to create the button and label objects in the main code,
> even though they are already declared in the UI XML file?
Only if you need to access them from Java.
> For
> example, the only way I could figure out to change the TextView text
> was to do the following lines in the onClickListener for the button:
>
> private OnClickListener gossamerButListener = new OnClickListener
> ()
> {
> public void onClick(View v)
> {
> TextView answerLabel = (TextView)findViewById
> (R.id.answerLabel);
> answerLabel.setText("Test");
> }
> };
>
> Is there a better way to do this?
Well, if the button will be clicked a lot, you might consider finding
answerLabel at the end of your Activity's onCreate(), so you don't have
to keep looking it up. findViewById() isn't glacial, but it's still not
cheap to call.
> Is there a way to directly access
> the TextView without using findViewbyId and casting it to the
> appropriate type?
If you are using layout XML, then findViewById() is your only option.
There's nothing preventing you from constructing Views in pure Java
code, of course. It's more tedious and verbose.
--
Mark Murphy (a Commons Guy)
http://commonsware.com
Android Training on the Ranch! -- Mar 16-20, 2009
http://www.bignerdranch.com/schedule.shtml
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" 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-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---