Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (9.04 MB, 688 trang )
Figure 1-51.
Discussion
File: main.xml
Example 1-26.
<?xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:id="@+id/button"
android:text="Start"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
android:padding="10dip"
android:layout_gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
android:id="@+id/timer"
android:text="Time: "
android:paddingRight="10dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:id="@+id/timeElapsed"
1.20 Program: A Simple CountDownTimer example | 71
www.it-ebooks.info
android:text="Time elapsed: "
android:paddingRight="10dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
File: Main.java
Example 1-27.
package com.examples;
import
import
import
import
import
import
import
android.app.Activity;
android.os.Bundle;
android.os.CountDownTimer;
android.view.View;
android.view.View.OnClickListener;
android.widget.Button;
android.widget.TextView;
public class Main extends Activity implements OnClickListener
{
private static final String tag = "Main";
private MalibuCountDownTimer countDownTimer;
private long timeElapsed;
private boolean timerHasStarted = false;
private Button startB;
private TextView text;
private TextView timeElapsedView;
private final long startTime = 50000;
private final long interval = 1000;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startB = (Button) this.findViewById(R.id.button);
startB.setOnClickListener(this);
}
text = (TextView) this.findViewById(R.id.timer);
timeElapsedView = (TextView) this.findViewById(R.id.timeElapsed);
countDownTimer = new MalibuCountDownTimer(startTime, interval);
text.setText(text.getText() + String.valueOf(startTime));
@Override
public void onClick(View v)
{
if (!timerHasStarted)
{
72 | Chapter 1: Getting Started
www.it-ebooks.info
countDownTimer.start();
timerHasStarted = true;
startB.setText("Start");
}
else
{
countDownTimer.cancel();
timerHasStarted = false;
startB.setText("RESET");
}
}
// CountDownTimer class
public class MalibuCountDownTimer extends CountDownTimer
{
public MalibuCountDownTimer(long startTime, long interval)
{
super(startTime, interval);
}
@Override
public void onFinish()
{
text.setText("Time's up!");
timeElapsedView.setText("Time Elapsed: " + String.valueOf(startTime));
}
}
}
@Override
public void onTick(long millisUntilFinished)
{
text.setText("Time remain:" + millisUntilFinished);
timeElapsed = startTime - millisUntilFinished;
timeElapsedView.setText("Time Elapsed: " + String.valueOf(timeElapsed));
}
The result looks like this:
Source Download URL
The source code for this example may be downloaded from this URL: http://www.file
factory.com/file/cbbbc38/n/SimpleCountDownTimerExample.zip
1.21 Program: Tipster, a tip calculator for the Android OS
Sunit Katkar
1.21 Program: Tipster, a tip calculator for the Android OS | 73
www.it-ebooks.info
Figure 1-52.
74 | Chapter 1: Getting Started
www.it-ebooks.info