Game Development Reference
In-Depth Information
@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
button= new Button( this );
button.setText( "Touch me!" );
button.setOnClickListener( this );
setContentView(button);
}
public void onClick(View v) {
touchCount++;
button.setText("Touched me "+touchCount+" time(s)");
}
}
Let's dissect Listing 2-1, so you can understand what it's doing. We'll leave the nitty-gritty details
for later chapters. All we want is to get a sense of what's happening.
The source code file starts with the standard Java package declaration and several imports.
Most Android framework classes are located in the android package.
package com.helloworld;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
Next, we define our HelloWorldActivity , and let it extend the base class Activity , which is
provided by the Android framework API. An Activity is a lot like a window in classical desktop
UIs, with the constraint that the Activity always fills the complete screen (except for the
notification bar at the top of the Android UI). Additionally, we let the Activity implement the
interface OnClickListener . If you have experience with other UI toolkits, you'll probably see
what's coming next. More on that in a second.
public class HelloWorldActivity extends Activity
implements View.OnClickListener {
We let our Activity have two members: a Button and an int that counts how often the Button
is touched.
Button button;
int touchCount;
Every Activity subclass must implement the abstract method Activity.onCreate() , which gets
called once by the Android system when the Activity is first started. This replaces a constructor
you'd normally expect to use to create an instance of a class. It is mandatory to call the base
class onCreate() method as the first statement in the method body.
@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
Next, we create a Button and set its initial text. Button is one of the many widgets that the
Android framework API provides. UI widgets are called views on Android. Note that button is a
member of our HelloWorldActivity class. We'll need a reference to it later on.
Search WWH ::




Custom Search