Android项目开发实战:倒计时
CountTimeMainActivity:
package com.example.counttime;
import java.util.Timer;
import java.util.TimerTask;
import android.R.integer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class CountTimeMainActivity extends Activity implements OnClickListener {
private EditText input;
private Button gettime;
private TextView time;
private Button start;
private Button stop;
private int i=0;
private Timer timer=null;
private TimerTask task=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_count_time_main);
InitView();
}
public void InitView(){
input=(EditText) findViewById(R.id.inputtime);
gettime=(Button) findViewById(R.id.gettime);
time=(TextView) findViewById(R.id.time);
start=(Button) findViewById(R.id.starttime);
stop=(Button) findViewById(R.id.stoptime);
gettime.setOnClickListener(this);
start.setOnClickListener(this);
stop.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.gettime:
time.setText(input.getText().toString());
i=Integer.parseInt(input.getText().toString());
break;
case R.id.starttime:
startTime();
break;
case R.id.stoptime:
stopTime();
break;
default:
break;
}
}
private Handler mhandler=new Handler(){
public void handleMessage(Message msg){
time.setText(msg.arg1+"");
startTime();
};
};
public void startTime(){
timer=new Timer();
task=new TimerTask() {
@Override
public void run() {
i--;
Message message=mhandler.obtainMessage();
message.arg1=i;
mhandler.sendMessage(message);
}
};
timer.schedule(task, 1000);
}
public void stopTime(){
timer.cancel();
}
}
import java.util.Timer;
import java.util.TimerTask;
import android.R.integer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class CountTimeMainActivity extends Activity implements OnClickListener {
private EditText input;
private Button gettime;
private TextView time;
private Button start;
private Button stop;
private int i=0;
private Timer timer=null;
private TimerTask task=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_count_time_main);
InitView();
}
public void InitView(){
input=(EditText) findViewById(R.id.inputtime);
gettime=(Button) findViewById(R.id.gettime);
time=(TextView) findViewById(R.id.time);
start=(Button) findViewById(R.id.starttime);
stop=(Button) findViewById(R.id.stoptime);
gettime.setOnClickListener(this);
start.setOnClickListener(this);
stop.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.gettime:
time.setText(input.getText().toString());
i=Integer.parseInt(input.getText().toString());
break;
case R.id.starttime:
startTime();
break;
case R.id.stoptime:
stopTime();
break;
default:
break;
}
}
private Handler mhandler=new Handler(){
public void handleMessage(Message msg){
time.setText(msg.arg1+"");
startTime();
};
};
public void startTime(){
timer=new Timer();
task=new TimerTask() {
@Override
public void run() {
i--;
Message message=mhandler.obtainMessage();
message.arg1=i;
mhandler.sendMessage(message);
}
};
timer.schedule(task, 1000);
}
public void stopTime(){
timer.cancel();
}
}
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".CountTimeMainActivity" >
<EditText
android:id="@+id/inputtime"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/gettime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="获取倒计时时间" />
<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/starttime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始计时" />
<Button
android:id="@+id/stoptime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止计时" />
</LinearLayout>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".CountTimeMainActivity" >
<EditText
android:id="@+id/inputtime"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/gettime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="获取倒计时时间" />
<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/starttime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始计时" />
<Button
android:id="@+id/stoptime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止计时" />
</LinearLayout>