android 倒计 代码,Android中倒计时代码

5167cf267695827cde81bd214fb36ed5.png

6c1e38c47aa06fbad7b88e5f6bed962a.png

7dbc9e486898fbeb998d0a3c226bf8f9.png

c1f016f5af83f0fefc7df4d33a5faee5.png

布局:

maina.xml

android:id="@+id/myClock"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentTop="true"

android:layout_centerHorizontal="true"

android:layout_margin="10dp"

android:textSize="30sp" />

android:id="@+id/text_select"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@id/myClock"

android:layout_centerHorizontal="true"

android:text="@string/text_select"

android:textSize="20sp" />

android:layout_width="60dp"

android:layout_height="80dp"

android:layout_below="@id/text_select"

android:layout_alignLeft="@id/myClock"

android:layout_marginTop="20dp"

android:inputType="number"

android:gravity="center"

/>

android:layout_width="60dp"

android:layout_height="80dp"

android:layout_below="@id/text_select"

android:layout_toRightOf="@id/minute"

android:layout_marginTop="20dp"

android:inputType="number"

android:gravity="center"

/>

android:id="@+id/button_start"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"

android:gravity="center"

android:padding="10dp"

android:text="@string/myButtonText"

android:textSize="30sp" />

start.xml

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_margin="30dp"

android:textSize="100sp"

android:textColor="#FF0000"

android:gravity="center"

android:textStyle="bold" />

MainActivity.java

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

}

}

});

}

@Override

protected void onResume() {

// TODO Auto-generated method stub

minute = 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);

}

}

}

}

};

};

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

}

}

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值