Write a countdown software for Android

Source: Internet
Author: User

When Ubuntu is upgraded to 11.04 blank files, write a small program trainer.

Let's take a look at the uidesign of the program:

 

 

Code for layout file 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 = "Countdown"

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>

 

 

 

Main program code:

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.tar t );

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 x 60*1000,100 0 ){

@ Override

Public void ontick (long millisuntilfinished ){

Time. settext (string. valueof (millisuntilfinished/1000) + "S ");

}

@ Override

Public void onfinish (){

Isbrewing = false;

Mmediaplayer. Start ();

Time. settext ("halilua ");

Start. settext ("START ");

}

};

Brewcountdowntimer. Start ();

Start. settext ("stop ");

Isbrewing = true;

}

Public void stopbrew (){

If (brewcountdowntimer! = NULL)

Brewcountdowntimer. Cancel ();

Isbrewing = false;

Start. settext ("START ");

}

 

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

}

}

}

 

I am too lazy to write comments. Some code comes from coolshell. If you are interested, you can look for the tea program above.

Finally, let me show you my ubuntu11.04.




Unity-based Ubuntu is just around the corner !!!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.