Android中倒计时代码_第1张图片Android中倒计时代码_第2张图片

Android中倒计时代码_第3张图片Android中倒计时代码_第4张图片

 

布局:

maina.xml

            


start.xml


MainActivity.java

public class MainActivity extends Activity {Button startButton;EditText minuteText;EditText secondText;int minute;int second;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.main);startButton = (Button) findViewById(R.id.button_start);minuteText = (EditText)findViewById(R.id.minute);secondText = (EditText)findViewById(R.id.second);startButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {if (!minuteText.getText().toString().equals("")) {minute = Integer.parseInt(minuteText.getText().toString());}if (!secondText.getText().toString().equals("")) {second = Integer.parseInt(secondText.getText().toString());}if (minute != 0 || second != 0) {System.out.println(minute+":"+second);ArrayList list = new ArrayList();list.add(minute);list.add(second);Intent intent = new Intent();intent.setAction("com.example.mytime.StartActivity");intent.putIntegerArrayListExtra("times", list);startActivity(intent);}}});}@Overrideprotected void onResume() {// TODO Auto-generated method stubminute = 0;second = 0;super.onResume();}}

StartActivity.java

public class StartActivity extends Activity{static int minute = -1;static int second = -1;final static String tag = "tag";TextView timeView;Timer timer;TimerTask  timerTask;Handler handler = new Handler(){public void handleMessage(Message msg) {System.out.println("handle!");if (minute == 0) {if (second == 0) {timeView.setText("Time out !");if (timer != null) {timer.cancel();timer = null;}if (timerTask != null) {timerTask = null;}}else {second--;if (second >= 10) {timeView.setText("0"+minute + ":" + second);}else {timeView.setText("0"+minute + ":0" + second);}}}else {if (second == 0) {second =59;minute--;if (minute >= 10) {timeView.setText(minute + ":" + second);}else {timeView.setText("0"+minute + ":" + second);}}else {second--;if (second >= 10) {if (minute >= 10) {timeView.setText(minute + ":" + second);}else {timeView.setText("0"+minute + ":" + second);}}else {if (minute >= 10) {timeView.setText(minute + ":0" + second);}else {timeView.setText("0"+minute + ":0" + second);}}}}};};@Overrideprotected void onCreate(Bundle savedInstanceState) {Log.v(tag, "log---------->onCreate!");requestWindowFeature(Window.FEATURE_NO_TITLE);super.onCreate(savedInstanceState);setContentView(R.layout.start);timeView = (TextView)findViewById(R.id.myTime);if (minute == -1 && second == -1) {Intent intent = getIntent();ArrayList times = intent.getIntegerArrayListExtra("times");minute = times.get(0);second = times.get(1);}timeView.setText(minute + ":" + second);timerTask = new TimerTask() {@Overridepublic void run() {Message msg = new Message();msg.what = 0;handler.sendMessage(msg);}};timer = new Timer();timer.schedule(timerTask,0,1000);}@Overrideprotected void onDestroy() {Log.v(tag, "log---------->onDestroy!");if (timer != null) {timer.cancel();timer = null;}if (timerTask != null) {timerTask = null;}minute = -1;second = -1;super.onDestroy();}@Overrideprotected void onStart() {Log.v(tag, "log---------->onStart!");super.onStart();}@Overrideprotected void onStop() {Log.v(tag, "log---------->onStop!");super.onStop();}@Overrideprotected void onResume() {Log.v(tag, "log---------->onResume!");super.onResume();}@Overrideprotected void onRestart() {Log.v(tag, "log---------->onRestart!");super.onRestart();}@Overrideprotected void onPause() {Log.v(tag, "log---------->onPause!");super.onPause();}}


本文本详细源码文件请访问:http://download.csdn.net/detail/chrp99/5646983

 

更多源码请访问:http://download.csdn.net/user/chrp99/uploads

 


 

 

更多相关文章

  1. android电子点餐系统源码
  2. Android聊天室(源码)
  3. 源码开发-小结
  4. android mediaplayer 音效 代码
  5. android小游戏代码
  6. Android WebView文件上传(关键代码)
  7. Android Surfaceflinger源码分析
  8. android,java代码设置背景色
  9. Android保存图片到图库,Android扫描文件到媒体库,Android保存图片

随机推荐

  1. 如何在AOSP构建中设置默认启动程序?
  2. 最新Eclipse配置Android开发环境SDK 并安
  3. android应用如何在有需要时申请root权限?
  4. Android混合应用程序:JQueryMobile ajax不
  5. Android MediaCodec硬解码AAC音频文件(实
  6. 【Android应用开发技术:应用组件】Android
  7. 自定义适配器的实现
  8. Gradle返回包不存在
  9. 与adb的连接已关闭,并且发生了严重错误[重
  10. Kotlin 学习之类和继承