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市场指日可待了!!!

更多相关文章

  1. [Android实例] Android网络收音机项目(内含源码)
  2. android:屏幕自适应
  3. Android开发人员不得不收集的代码(blankj:utilcode)
  4. 从零开始学Xamarin.Forms(四) Android(安卓)准备步骤(添加第三方X
  5. Android(安卓)Studio插件整理
  6. android之startActivityForResult的使用
  7. 刚开始安卓,记录一个刚做的图片缩放程序
  8. Android(安卓)近百个项目的源代码,覆盖Android开发的每个领域
  9. Android选项切换条SHSegmentControl

随机推荐

  1. 谈谈 Android 中的 PathClassLoader 和 D
  2. android:windowSoftInputMode属性详解
  3. 低功耗蓝牙(BLE)之概念理解
  4. Android(安卓)Camera模块分析
  5. android应用安全——数据安全
  6. Android之辅助服务上篇————Accessibi
  7. Android NDK系列(三)-AS编写C文件没有提示
  8. 【Android 界面效果19】Android中shape的
  9. Android中调用C++函数的一个简单Demo
  10. Android Test - Auto Test Multi Activit