Android开发之小程序-秒表
2014-04-23 14:34
288 查看
原文链接:/article/1470860.html
手机没有秒表,自己想做一个秒表来给自己用,现在马上做出一个实例来,这只是开始,以后做个界面漂亮的应用出来。废话不说,先上图:
源码:
建立项目:Stopwatch
代码清单:org/wwj/Stopwatch/Stopwatch.java
[java] view
plaincopy
package org.wwj.Stopwatch;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Stopwatch extends Activity {
private TextView minText; //分
private TextView secText; //秒
private Button start; //开始按钮
private Button stop; //停止按钮
private boolean isPaused = false;
private String timeUsed;
private int timeUsedInsec;
private Handler uiHandle = new Handler(){
public void handleMessage(android.os.Message msg) {
switch(msg.what){
case 1:
if(!isPaused) {
addTimeUsed();
updateClockUI();
}
uiHandle.sendEmptyMessageDelayed(1, 1000);
break;
default: break;
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stopwatch);
//获取界面的控件
minText = (TextView) findViewById(R.id.min);
secText = (TextView) findViewById(R.id.sec);
start = (Button) findViewById(R.id.start);
stop = (Button) findViewById(R.id.stop);
//为按钮Start注册监听器
start.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
uiHandle.removeMessages(1);
startTime();
isPaused = false;
}
});
//为按钮stop注册监听器
stop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
isPaused = true;
timeUsedInsec = 0;
}
});
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
isPaused = true;
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
isPaused = false;
}
private void startTime(){
uiHandle.sendEmptyMessageDelayed(1, 1000);
}
/**
* 更新时间的显示
*/
private void updateClockUI(){
minText.setText(getMin() + ":");
secText.setText(getSec());
}
public void addTimeUsed(){
timeUsedInsec = timeUsedInsec + 1;
timeUsed = this.getMin() + ":" + this.getSec();
}
public CharSequence getMin(){
return String.valueOf(timeUsedInsec / 60);
}
public CharSequence getSec(){
int sec = timeUsedInsec % 60;
return sec < 10? "0" + sec :String.valueOf(sec);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_stopwatch, menu);
return true;
}
}
界面布局:res/layout/layout_stopwatch.xml
[html] view
plaincopy
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<TextView
android:id="@+id/min"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"/>
<TextView
android:id="@+id/sec"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
android:textColor="#ff0000"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
>
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/start"/>
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/stop"/>
</LinearLayout>
</LinearLayout>
手机没有秒表,自己想做一个秒表来给自己用,现在马上做出一个实例来,这只是开始,以后做个界面漂亮的应用出来。废话不说,先上图:
源码:
建立项目:Stopwatch
代码清单:org/wwj/Stopwatch/Stopwatch.java
[java] view
plaincopy
package org.wwj.Stopwatch;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Stopwatch extends Activity {
private TextView minText; //分
private TextView secText; //秒
private Button start; //开始按钮
private Button stop; //停止按钮
private boolean isPaused = false;
private String timeUsed;
private int timeUsedInsec;
private Handler uiHandle = new Handler(){
public void handleMessage(android.os.Message msg) {
switch(msg.what){
case 1:
if(!isPaused) {
addTimeUsed();
updateClockUI();
}
uiHandle.sendEmptyMessageDelayed(1, 1000);
break;
default: break;
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stopwatch);
//获取界面的控件
minText = (TextView) findViewById(R.id.min);
secText = (TextView) findViewById(R.id.sec);
start = (Button) findViewById(R.id.start);
stop = (Button) findViewById(R.id.stop);
//为按钮Start注册监听器
start.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
uiHandle.removeMessages(1);
startTime();
isPaused = false;
}
});
//为按钮stop注册监听器
stop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
isPaused = true;
timeUsedInsec = 0;
}
});
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
isPaused = true;
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
isPaused = false;
}
private void startTime(){
uiHandle.sendEmptyMessageDelayed(1, 1000);
}
/**
* 更新时间的显示
*/
private void updateClockUI(){
minText.setText(getMin() + ":");
secText.setText(getSec());
}
public void addTimeUsed(){
timeUsedInsec = timeUsedInsec + 1;
timeUsed = this.getMin() + ":" + this.getSec();
}
public CharSequence getMin(){
return String.valueOf(timeUsedInsec / 60);
}
public CharSequence getSec(){
int sec = timeUsedInsec % 60;
return sec < 10? "0" + sec :String.valueOf(sec);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_stopwatch, menu);
return true;
}
}
界面布局:res/layout/layout_stopwatch.xml
[html] view
plaincopy
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<TextView
android:id="@+id/min"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"/>
<TextView
android:id="@+id/sec"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
android:textColor="#ff0000"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
>
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/start"/>
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/stop"/>
</LinearLayout>
</LinearLayout>
相关文章推荐
- 开发Android Map程序 如何获取 apikey (Google Map API v2)
- Android开发之HelloWorld程序
- Android开发模板------再按一次退出程序 KeyEvent.KEYCODE_BACK
- 一个Android位置监管程序的开发全流程
- 【Android开发经验】我们要友好的告诉用户,程序要崩溃了
- 传智播客开设Unity3d游戏开发及.Net开发Android程序课程
- android平台开发voip类程序的探索
- Android 开发前的基本的配置及第一个Android 程序
- 写给Android开发者看的『微信小程序和Android开发的对比』
- Mac OS X 下开发 Android 程序时使用 USB 连真机调试
- Adobe Flash Builder 4.5 Android Air 程序开发系列 之八 照相机
- cygwin配合NDK开发Android程序
- kotlin 开发 android 程序中网络http请求和线程的使用
- android开发中WebView的使用(附完整程序)
- Android开发(一)发送短信程序
- Android adt bundle 开发环境配置及第一个“Hello world”程序运行
- Eclipse开发Android程序引入jar包和so文件
- 【分享】android开发该如何提高Android程序的效率
- windows下eclipse开发android程序识别android设备
- ubuntu下整合eclipse和javah生成jni头文件开发android的native程序(转)