写个android下倒计时软件

ubuntu升级到11.04的空档时间写个小程序练练手。

先看看程序UI设计:

布局文件main.xml的代码:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

  xmlns:android="http://schemas.android.com/apk/res/android"

  android:orientation="vertical"

  android:layout_width="fill_parent"

  android:layout_height="fill_parent">

  <LinearLayout

    android:layout_width="fill_parent"

    android:layout_height="140dip">

    <TextView

    android:gravity="center"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:text="倒计时"

    android:textSize="90dip"/>

  </LinearLayout>

  <LinearLayout

    android:orientation="horizontal"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:layout_weight="1"

    android:gravity="center"

    android:padding="10dip">

    <Button

      android:id="@+id/down"

      android:layout_width="wrap_content"

      android:layout_height="wrap_content"

      android:text="-"

      android:textSize="40dip" />

    <TextView

      android:id="@+id/time"

      android:layout_width="wrap_content"

      android:layout_height="wrap_content"

      android:text="0:00"

      android:textSize="40dip"

      android:padding="10dip" />

    <Button

      android:id="@+id/up"

      android:layout_width="wrap_content"

      android:layout_height="wrap_content"

      android:text="+"

      android:textSize="40dip" />

  </LinearLayout>

  <Button

    android:id="@+id/tart"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:layout_gravity="bottom"

    android:text="开始"/>

</LinearLayout>

程序主代码:

package android.daojishi;

import android.app.Activity;

import android.media.MediaPlayer;

import android.os.Bundle;

import android.os.CountDownTimer;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.TextView;

public class BrewClockActivity extends Activity implements OnClickListener {

  protected Button AddTime;

  protected Button DecreaseTime;

  protected Button start;

  protected TextView Time;

  MediaPlayer mMediaPlayer;

  protected int brewTime = 3;

  protected CountDownTimer brewCountDownTimer;

  protected int brewCount = 0;

  protected boolean isBrewing = false;

  

  @Override

  public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    initSounds();

    setContentView(R.layout.main);

    // Connect interface elements to properties

    AddTime = (Button) findViewById(R.id.up);

    DecreaseTime = (Button) findViewById(R.id.down);

    start = (Button) findViewById(R.id.tart);

    Time = (TextView) findViewById(R.id.time);

    

    // Setup ClickListeners

    AddTime.setOnClickListener(this);

    DecreaseTime.setOnClickListener(this);

    start.setOnClickListener(this);

    

    // Set the initial brew values

    setBrewTime(3);

  }

  private void initSounds() {

// TODO Auto-generated method stub

 mMediaPlayer=MediaPlayer.create(this, R.raw.haliluya);

 

}

public void setBrewTime(int minutes) {

    if(isBrewing)

      return;

    

    brewTime = minutes;

    

    if(brewTime < 1)

      brewTime = 1;

    Time.setText(String.valueOf(brewTime) + "m");

  }

  

  public void startBrew() {

    brewCountDownTimer = new CountDownTimer(brewTime * 60 * 1000, 1000) {

      @Override

      public void onTick(long millisUntilFinished) {

        Time.setText(String.valueOf(millisUntilFinished / 1000) + "s");

      }

      

      @Override

      public void onFinish() {

        isBrewing = false;

        mMediaPlayer.start();

        Time.setText("哈利路亚");

        start.setText("开始");

      }

    };

    

    brewCountDownTimer.start();

    start.setText("停止");

    isBrewing = true;

  }

  

  public void stopBrew() {

    if(brewCountDownTimer != null)

      brewCountDownTimer.cancel();

    

    isBrewing = false;

    start.setText("开始");

  }

  public void onClick(View v) {

    if(v == AddTime)

      setBrewTime(brewTime + 1);

    else if(v == DecreaseTime)

      setBrewTime(brewTime -1);

    else if(v == start) {

      if(isBrewing)

        stopBrew();

      else

        startBrew();

    }

  }

}

懒得写注解,部分代码来自coolshell,有兴趣的童鞋可以去找找上面那个泡茶程序。

最后允许显摆一下我的ubuntu11.04






基于unity的ubuntu怕是一统桌面PC市场指日可待了!!!

原文地址:https://www.cnblogs.com/myphoebe/p/2154054.html