Книга: 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.

Оглавление книги


Генерация: 0.582. Запросов К БД/Cache: 2 / 0
поделиться
Вверх Вниз