80,472
社区成员




<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.stopwatch"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".StopWatch"
android:label="StopWatch by XGW">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity"
android:label="Stopped">
</activity>
</application>
</manifest>
import com.example.stopwatch.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.*;
public class StopWatch extends Activity{
final Calendar cal = Calendar.getInstance();
int seconds;
long startTime, t2;
boolean started = false;
int second;
int minute;
ArrayList<Integer> ListOfSecond = new ArrayList<Integer>();
public String showTimeCount(long time) {
String timeCount = "";
long hourc = time/3600000;
String hour = "0" + hourc;
hour = hour.substring(hour.length()-2, hour.length());
long minuec = (time-hourc*3600000)/(60000);
String minue = "0" + minuec;
minue = minue.substring(minue.length()-2, minue.length());
long secc = (time-hourc*3600000-minuec*60000)/1000;
String sec = "0" + secc;
sec = sec.substring(sec.length()-2, sec.length());
timeCount = hour + ":" + minue + ":" + sec;
return timeCount;
}
class Timer extends Thread{
String temp;
long startTime;
Timer(long time){
startTime = time;
}
public void run(){
try{
while(true){
cal.setTime(new Date());
long currentTime = cal.getTimeInMillis();
temp = showTimeCount(currentTime - startTime);
text.setText(temp);
Thread.sleep(1000);
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
Button button1;
Button button2;
Button button3;
TextView text;
protected void onCreat(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.first_layout);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
text = (TextView) findViewById(R.id.textView1);
button1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
if(started == false){
cal.setTime(new Date());
startTime = cal.getTimeInMillis();
started = true;
Timer timer = new Timer(startTime);
timer.start();
}
else {
Toast.makeText(getBaseContext(), "you have already started it!", Toast.LENGTH_LONG).show();
}
}
});
Log.e("1","1");
button2.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
cal.setTime(new Date());
long t2= cal.getTimeInMillis();
seconds = (int) ((t2 - startTime) % 1000);
ListOfSecond.add(seconds);
Toast.makeText(getBaseContext(), "time recorded.", Toast.LENGTH_SHORT).show();
}
});
button3.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Toast.makeText(getBaseContext(), "stopped.", Toast.LENGTH_LONG).show();
Bundle data = new Bundle();
Intent intent = new Intent(StopWatch.this, SecondActivity.class);
data.putIntegerArrayList("second",ListOfSecond);
intent.putExtras(data);
startActivity(intent);
}
});
}
}
import java.util.ArrayList;
import java.util.Date;
import android.util.Log;
import com.example.stopwatch.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class SecondActivity extends Activity{
public String showTimeCount(long time) {
String timeCount = "";
long hourc = time/3600;
String hour = "0" + hourc;
hour = hour.substring(hour.length()-2, hour.length());
long minuec = (time-hourc*3600);
String minue = "0" + minuec;
minue = minue.substring(minue.length()-2, minue.length());
long secc = (time-hourc*3600-minuec*60);
String sec = "0" + secc;
sec = sec.substring(sec.length()-2, sec.length());
timeCount = hour + ":" + minue + ":" + sec;
return timeCount;
}
TextView text;
Button button1;
protected void onCreate(Bundle saveInstanceState){
super.onCreate(saveInstanceState);
setContentView(R.layout.second_layout);
text = (TextView) findViewById(R.id.textView1);
button1 = (Button) findViewById(R.id.button1);
Bundle bundle = this.getIntent().getExtras();
ArrayList<Integer> ListOfSecond = bundle.getIntegerArrayList("second");
String display = "";
for (int i= 0; i < ListOfSecond.size(); ++ i){
display += showTimeCount(ListOfSecond.get(i));
display += '\n';
}
text.setText(display);
button1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent(SecondActivity.this, StopWatch.class);
startActivity(intent);
}
});
}
}