Книга: Beginning Android
The Activity
The Activity
Your project’s src/ directory contains the standard Java-style tree of directories based upon the Java package you used when you created the project (i.e., com.commonsware.android
resulted in src/com/commonsware/android/
). Inside the innermost directory you should find a pregenerated source file named Now.java
, which is where your first activity will go. This activity will contain a single button that displays the time the button was last pushed (or the time the application was started if the button hasn’t been pushed).
Open Now.java
in your editor and paste in the following code:
package com.commonsware.android.skeleton;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.util.Date;
public class Now extends Activity implements View.OnClickListener {
Button btn;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
btn = new Button(this);
btn.setOnClickListener(this);
updateTime();
setContentView(btn);
}
public void onClick(View view) {
updateTime();
}
private void updateTime() {
btn.setText(new Date().toString());
}
}
Or, if you download the source files off the CommonsWare Web site, you can just use the Skeleton/Now
project directly.
Let’s examine this piece-by-piece.
- It’s All The Same, Just Different
- 15.6.1 Synchronous Activity Synchronization
- The netstat Command
- Checking the status, speed, and activity for network connections
- Using debug logging to track DNS activity
- 15.2.2 Activity Synchronization
- The Grace of State
- Monitoring User Activity on the System
- Dissecting the Activity
- Building and Running the Activity
- Craft the Search Activity
- Using Basic Print Commands