关闭

计时器

82人阅读 评论(0) 收藏 举报
分类:

自定义TextView实现DigitalClock的效果,加上日期

package com.huazi.MyTimer;


import java.util.Calendar;


import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.database.ContentObserver;
import android.os.Handler;
import android.os.SystemClock;
import android.provider.Settings;
import android.text.format.DateFormat;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.TextView;


public class TextWidget extends TextView {
   Calendar mCalendar;
   String mFormat;
   private Handler mHandler;
   private Runnable mTicker;
   private static final String m12 = "h:mm:ss";
   private static final String m24 = "k:mm:ss";
   private boolean mTickerStopped = false;
   private FormatChangeObserver mFormatChangeObserver;
   private static String[] weekdays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
//   private FormatChangeObserver mFormatChangeObserver;
public TextWidget(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// TODO Auto-generated constructor stub
initClock(context);
}
public TextWidget(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
initClock(context);
}

public TextWidget(Context context) {
super(context);
// TODO Auto-generated constructor stub
initClock(context);
}
private void initClock(Context context) {
// TODO Auto-generated method stub
Resources r = context.getResources();
if(mCalendar == null){
mCalendar = Calendar.getInstance();
}
mFormatChangeObserver = new FormatChangeObserver();
           //注册,监听系统日期设置数据库的改变
            getContext().getContentResolver().registerContentObserver(Settings.System.CONTENT_URI, true, mFormatChangeObserver);
setFormat();
}
private void setFormat() {
// TODO Auto-generated method stub
if(get24HourMode()){
mFormat = m24;
}else{
mFormat = m12;
}
}
private boolean get24HourMode() {
// TODO Auto-generated method stub
return android.text.format.DateFormat.is24HourFormat(getContext());
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub

getContext().startActivity(new Intent(android.provider.Settings.ACTION_DATE_SETTINGS));
return super.onTouchEvent(event);
}
private String format(int myear) {
// TODO Auto-generated method stub
String s  = ""+myear;
if(s.length()==1){
s = "0" + s;
}
return s;
}


@Override
protected void onAttachedToWindow() {
// TODO Auto-generated method stub
mTickerStopped = false;
super.onAttachedToWindow();
mHandler = new Handler();
mTicker = new Runnable(){


@Override
public void run() {
// TODO Auto-generated method stub
if(mTickerStopped)return;
mCalendar.setTimeInMillis(System.currentTimeMillis());
int myear = mCalendar.get(Calendar.YEAR);
int mmonth = mCalendar.get(Calendar.MONTH)+1;
int mmonthday = mCalendar.get(Calendar.DAY_OF_MONTH);
int mweekday = mCalendar.get(Calendar.DAY_OF_WEEK)-1;
String mDate = format(myear)+"-"+format(mmonth)+"-"+format(mmonthday);
setText(DateFormat.format(mFormat, mCalendar) + "  " + mDate + " " + weekdays[mweekday]);
invalidate();
                 long now = SystemClock.uptimeMillis();
                 long next = now + (1000 - now % 1000);
                 mHandler.postAtTime(mTicker, next);
}




};
mTicker.run();
}


private class FormatChangeObserver extends ContentObserver {


public FormatChangeObserver() {
super(new Handler());
// TODO Auto-generated constructor stub
}
@Override
         public void onChange(boolean selfChange) {
         setFormat();
         }

}




@Override
protected void onDetachedFromWindow() {
// TODO Auto-generated method stub
super.onDetachedFromWindow();
mTickerStopped = true;
}





}


调用自定义TextView

package com.huazi.MyTimer;


import java.util.ArrayList;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;


public class MainActivity extends Activity {


Button startButton;
EditText minuteText;
EditText secondText;
int minute;
int second;

@Override
public 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() {


@Override
public 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<Integer> list = new ArrayList<Integer>();
list.add(minute);
list.add(second);

Intent intent = new Intent();
intent.setAction("com.huazi.MyTimer.StartActivity");

intent.putIntegerArrayListExtra("times", list);
startActivity(intent);
}
}
});
}

@Override
protected void onResume() {
// TODO Auto-generated method stub
minute = 0;
second = 0;
super.onResume();
}
}


开始倒计时:

package com.huazi.MyTimer;


import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Window;
import android.widget.TextView;


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);
}
}
}
}
};
};


@Override
protected 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<Integer> times = intent.getIntegerArrayListExtra("times");
minute = times.get(0);
second = times.get(1);
}

timeView.setText(minute + ":" + second);

timerTask = new TimerTask() {

@Override
public void run() {
Message msg = new Message();
msg.what = 0;
handler.sendMessage(msg);
}
};

timer = new Timer();
timer.schedule(timerTask,0,1000);

}

@Override
protected 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();
}

@Override
protected void onStart() {
Log.v(tag, "log---------->onStart!");
super.onStart();
}

@Override
protected void onStop() {
Log.v(tag, "log---------->onStop!");
super.onStop();
}


@Override
protected void onResume() {
Log.v(tag, "log---------->onResume!");
super.onResume();
}

@Override
protected void onRestart() {
Log.v(tag, "log---------->onRestart!");
super.onRestart();
}

@Override
protected void onPause() {
Log.v(tag, "log---------->onPause!");
super.onPause();
}

}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:18443次
    • 积分:481
    • 等级:
    • 排名:千里之外
    • 原创:22篇
    • 转载:48篇
    • 译文:1篇
    • 评论:3条
    文章分类
    最新评论