Android Multi-function clock development case (basic article) _android

Source: Internet
Author: User

This article we enter the Android multi-function clock development combat learning, the specific effect can refer to the phone on the clock, the contents are as follows
First, let's take a look at the layout file Layout_main.xml

Entire layout:

<framelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:id= "@+id/container" Android: Layout_width= "Match_parent" android:layout_height= "match_parent" > <tabhost android:id= "@android: Id/tabhost "Android:layout_width=" match_parent "android:layout_height=" match_parent "> <linearlayout android:la Yout_width= "Match_parent" android:layout_height= "match_parent" android:orientation= "vertical" > <TabWi  Dget android:id= "@android: Id/tabs" android:layout_width= "match_parent" android:layout_height= "Wrap_content" > </TabWidget> <framelayout android:id= "@android: Id/tabcontent" Android:layout_width= "mat Ch_parent "android:layout_height=" match_parent "> <com.example.clock.timeview android:id=" @+id/tab Time "android:layout_width=" Match_parent "android:layout_height=" match_parent "android:orientation=" vert ical "> </com.example.clock.timeview> <com.example.clock.alarmview android:id= "@+id/tabalarm" android:layout_width= " Match_parent "android:layout_height=" match_parent "android:orientation=" vertical "> <span style=" white- Space:pre "> </span&gt ... </com.example.clock.AlarmView> <com.example.clock.timerview and Roid:id= "@+id/tabtimer" android:layout_width= "match_parent" android:layout_height= "Match_parent" Android : orientation= "Vertical" > <span style= "white-space:pre" > </span&gt ... </com.example.clock.timerview& 
 
    Gt <com.example.clock.stopwatchview android:id= "@+id/tabstopwatch" android:layout_width= "Match_parent" a ndroid:layout_height= "match_parent" android:orientation= "vertical" > <span style= "White-space:pre" > </ Span> </com.example.clock.StopWatchView> </FrameLayout> </LinearLayout> </tabhost& 
 
Gt </fRamelayout>  

The whole layout is a framelayout, we put a tabhost in it, then we can add directly to the inside of the layout you want, may be a beginner to see a question that is <com.example.clock.......> </com.example.clock.......> What is this thing?? This is a custom control, we created a class that inherits the LinearLayout (the explanation can refer to here http://www.jb51.net/article/85515.htm), above we see four such labels, Indicates that we have four tab pages like this. About the layout of things here is not much to say, and then will I learn in the process of some do not understand, and related knowledge points uploaded to the resources, you can download to see.

Complete Layout file code:

<framelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:id= "@+id/container" Android: Layout_width= "Match_parent" android:layout_height= "match_parent" > <tabhost android:id= "@android: Id/tabhost "Android:layout_width=" match_parent "android:layout_height=" match_parent "> <linearlayout android:la Yout_width= "Match_parent" android:layout_height= "match_parent" android:orientation= "vertical" > <TabWi  Dget android:id= "@android: Id/tabs" android:layout_width= "match_parent" android:layout_height= "Wrap_content" > </TabWidget> <framelayout android:id= "@android: Id/tabcontent" Android:layout_width= "mat Ch_parent "android:layout_height=" match_parent "> <com.example.clock.timeview android:id=" @+id/tab Time "android:layout_width=" Match_parent "android:layout_height=" match_parent "android:orientation=" vert ical "> <textvieW android:id= "@+id/tvtime" android:layout_width= "fill_parent" android:layout_height= "Fill_parent" android:gravity= "center" android:textappearance= "Android:attr/textappearancelarge"/> </com.example . Clock. timeview> <com.example.clock.alarmview android:id= "@+id/tabalarm" android:layout_width= "Match_par" Ent "android:layout_height=" match_parent "android:orientation=" vertical "> <listview andr Oid:id= "@+id/lvlistalarm" android:layout_width= "fill_parent" android:layout_height= "0DP" Android:layo ut_weight= "1" > </ListView> <button android:id= "@+id/btnaddalarm" Android:layout_w Idth= "Fill_parent" android:layout_height= "wrap_content" android:text= "@string/add_alarm" > </but 
     ton> </com.example.clock.AlarmView> <com.example.clock.timerview android:id= "@+id/tabtimer" Android:layOut_width= "Match_parent" android:layout_height= "match_parent" android:orientation= "vertical" > <  
      LinearLayout android:layout_width= "fill_parent" android:layout_height= "0DP" android:layout_weight= "1" android:orientation= "Horizontal" > <edittext android:id= "@+id/ethour" android:layou T_width= "0DP" android:layout_height= "Wrap_content" android:layout_weight= "1" android:inputtype= "Nu 
 
      Mber "android:singleline=" true "android:textappearance="? Android:attr/textappearancelarge/> <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:text = ":" "android:textappearance=" Android:attr/textappearancelarge "/> <edittext android:id=" @+ Id/etmin "android:layout_width=" 0DP "android:layout_height=" Wrap_content "android:layout_weight=" 1 "Android:inputtYpe= "Number" android:singleline= "true" android:textappearance= "? Android:attr/textappearancelarge"/> <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Andro id:text= ":" android:textappearance= "Android:attr/textappearancelarge"/> <edittext Android : id= "@+id/etsec" android:layout_width= "0DP" android:layout_height= "Wrap_content" Android:layout_we ight= "1" android:inputtype= "number" android:singleline= "true" android:textappearance= "? Android:att 
      R/textappearancelarge "/> </LinearLayout> <linearlayout android:id=" @+id/btngroup " Android:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:orientation= "Horizontal" &G 
 
      T <button android:id= "@+id/btnstart" android:layout_width= "0DP" android:layout_height= "Wrap_conte" 
       ntandroid:layout_weight= "1" android:text= "@string/start"/> <button android:id= "@+id/btnpause 
       "Android:layout_width=" 0DP "android:layout_height=" Wrap_content "android:layout_weight=" 1 " android:text= "@string/pause"/> <button android:id= "@+id/btnresume" android:layout_width=  "0DP" android:layout_height= "Wrap_content" android:layout_weight= "1" android:text= "@string/resume" /> <button android:id= "@+id/btnreset" android:layout_width= "0DP" android:layout_he ight= "Wrap_content" android:layout_weight= "1" android:text= "@string/reset"/> &LT;/LINEARLAYOUT&G 
    T 
     </com.example.clock.TimerView> <com.example.clock.stopwatchview android:id= "@+id/tabstopwatch" 
 
     Android:layout_width= "Match_parent" android:layout_height= "match_parent" android:orientation= "vertical" > <lineArlayout android:layout_width= "fill_parent" android:layout_height= "Wrap_content" android:orientation= "Horizontal" > <textview android:id= "@+id/timehour" android:layout_width= "0DP" Andr oid:layout_height= "Wrap_content" android:layout_weight= "1" android:textappearance= "? Android:attr/textAppea Rancelarge "/> <textview android:layout_width=" wrap_content "android:layout_height=" Wrap_co Ntent "android:text=": "android:textappearance="? Android:attr/textappearancelarge "/> <Text 
       View android:id= "@+id/timemin" android:layout_width= "0DP" android:layout_height= "Wrap_content" android:layout_weight= "1" android:textappearance= "Android:attr/textappearancelarge"/> <TextV 
       Iew android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= ":" Android:textappearanCe= "Android:attr/textappearancelarge"/> <textview android:id= "@+id/timesec" android:layou T_width= "0DP" android:layout_height= "Wrap_content" android:layout_weight= "1" Android:textappearanc E= "Android:attr/textappearancelarge"/> <textview android:layout_width= "Wrap_content" Andr 
       oid:layout_height= "Wrap_content" android:text= "." 
       Android:textappearance= "Android:attr/textappearancelarge"/> <textview android:id= "@+id/timeMsec" 
       Android:layout_width= "0DP" android:layout_height= "Wrap_content" android:layout_weight= "1" Android:textappearance= "Android:attr/textappearancelarge"/> </LinearLayout> <listview A Ndroid:id= "@+id/lvwatchtime" android:layout_width= "fill_parent" android:layout_height= "0DP" android:l ayout_weight= "1" > </ListView> <linearlayout anDroid:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:orientation= "Horizontal" > <button android:id= "@+id/btnswstart" android:layout_width= "0DP" android:layout_height= "Wrap_content" android:layout_weight= "1" android:text= "@string/start"/> <button an Droid:id= "@+id/btnswpause" android:layout_width= "0DP" android:layout_height= "Wrap_content" Android 
       : layout_weight= "1" android:text= "@string/pause"/> <button android:id= "@+id/btnswresume" Android:layout_width= "0DP" android:layout_height= "Wrap_content" android:layout_weight= "1" an droid:text= "@string/resume"/> <button android:id= "@+id/btnswlap" android:layout_width= "0DP 
 
      "Android:layout_height=" Wrap_content "android:layout_weight=" 1 "android:text=" @string/lap "/> 
   <button    Android:id= "@+id/btnswreset" android:layout_width= "0DP" android:layout_height= "Wrap_content" a ndroid:layout_weight= "1" android:text= "@string/reset"/> </LinearLayout> </com.example.cloc  k.stopwatchview> </FrameLayout> </LinearLayout> </TabHost> </FrameLayout>

The layout is finished, so let's talk about Mainactivity

 private tabhost tabhost; 
 @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
 
 Setcontentview (R.layout.activity_main); Tabhost = (tabhost) Findviewbyid (Android. 
 R.id.tabhost); 
 
 Tabhost.setup (); Add a label for tabhost//Create a new Newtabspec (NEWTABSPEC) to specify the ID of the label (which is used to differentiate the label)/set its label and chart (setindicator)//set content (SetContent)/ * * Settings tab:--Set button name: Setindicator (clock); 
  --Set Tab content: SetContent (), * You can set the view component, you can set the activity, you can set the fragement; 
 */Tabhost.addtab (Tabhost.newtabspec ("Tabtime"). Setindicator ("Clock"). SetContent (R.id.tabtime)); 
 Tabhost.addtab (Tabhost.newtabspec ("Tabalarm"). Setindicator ("Alarm Clock"). SetContent (R.id.tabalarm)); 
 Tabhost.addtab (Tabhost.newtabspec ("Tabtimer"). Setindicator ("Timer"). SetContent (R.id.tabtimer)); 
Tabhost.addtab (Tabhost.newtabspec ("Tabstopwatch"). Setindicator ("Stopwatch"). SetContent (R.id.tabstopwatch)); } 

In mainactivity the main operation is to set the Tabhost, the above code has been posted on the explanation, here is not to say, the next one we will focus on the clock, alarm clock, timer and stopwatch these four parts, I hope you continue to learn.

This is the entire content of this article, I hope to learn more about Android software programming help.

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.