Android多功能时钟开发案例(基础篇)

 更新时间:2022年01月12日 07:59:46   投稿:lijiao  
这篇文章主要为大家详细介绍了Android多功能时钟开发案例的基础知识,为开发Android时钟打下基础,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文我们进入Android多功能时钟开发实战学习,具体的效果可以参考手机上的时钟,内容如下

首先我们来看一看布局文件layout_main.xml

整个布局:

<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:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:orientation="vertical" > 
 
  <TabWidget 
  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="match_parent" 
  android:layout_height="match_parent" > 
 
  <com.example.clock.TimeView 
   android:id="@+id/tabTime" 
   android:layout_width="match_parent" 
   android:layout_height="match_parent" 
   android:orientation="vertical" > 
  </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>…… 
  </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" > 
<span style="white-space:pre"> </span>…… 
  </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" > 
<span style="white-space:pre"> </span>…… 
  </com.example.clock.StopWatchView> 
  </FrameLayout> 
 </LinearLayout> 
 </TabHost> 
 
</FrameLayout> 

 整个布局整的是一个FrameLayout,我们在里面放了一个TabHost,接下来我们就可以在里面直接添加自己想要的布局了,可能初学者初一看会有那么一个疑问,就是<com.example.clock.……></com.example.clock.……>这个是什么东西??这是一个自定义的控件,我们创建的一个继承了LinearLayout的一个类(讲解可以参考这里//www.jb51.net/article/85515.htm),上面我们看到了四个这样的标签,表示我们有四个这样的Tab页面。关于布局的东西这里就不多讲了,之后会把我自己在学习过程中的一些不懂,以及相关的知识点上传到资源中,大家可以下载来看看。

完整的布局文件代码:

<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:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:orientation="vertical" > 
 
  <TabWidget 
  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="match_parent" 
  android:layout_height="match_parent" > 
 
  <com.example.clock.TimeView 
   android:id="@+id/tabTime" 
   android:layout_width="match_parent" 
   android:layout_height="match_parent" 
   android:orientation="vertical" > 
 
   <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_parent" 
   android:layout_height="match_parent" 
   android:orientation="vertical" > 
 
   <ListView 
   android:id="@+id/lvListAlarm" 
   android:layout_width="fill_parent" 
   android:layout_height="0dp" 
   android:layout_weight="1" > 
   </ListView> 
 
   <Button 
   android:id="@+id/btnAddAlarm" 
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content" 
   android:text="@string/add_alarm" > 
   </Button> 
  </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: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" 
    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" 
    android:text=":" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 
 
   <EditText 
    android:id="@+id/etSec" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:inputType="number" 
    android:singleLine="true" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 
   </LinearLayout> 
 
   <LinearLayout 
   android:id="@+id/btnGroup" 
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content" 
   android:orientation="horizontal" > 
 
   <Button 
    android:id="@+id/btnStart" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android: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_height="wrap_content" 
    android:layout_weight="1" 
    android:text="@string/reset" /> 
   </LinearLayout> 
  </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" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 
 
   <TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text=":" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 
 
   <TextView 
    android:id="@+id/timeMin" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 
 
   <TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text=":" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 
 
   <TextView 
    android:id="@+id/timeSec" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 
 
   <TextView 
    android:layout_width="wrap_content" 
    android: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 
   android:id="@+id/lvWatchTime" 
   android:layout_width="fill_parent" 
   android:layout_height="0dp" 
   android:layout_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 
    android: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" 
    android: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" 
    android:layout_weight="1" 
    android:text="@string/reset" /> 
   </LinearLayout> 
  </com.example.clock.StopWatchView> 
  </FrameLayout> 
 </LinearLayout> 
 </TabHost> 
 
</FrameLayout> 

讲完了布局,我们来讲讲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(); 
 
 // 为TabHost添加标签 
 // 新建一个newTabSpec(newTabSpec)用来指定该标签的id(就是用来区分标签)的 
 // 设置其标签和图表(setIndicator) 
 // 设置内容(setContent) 
 /* 
 * 设置选项卡 : -- 设置按钮名称 : setIndicator(时钟); -- 设置选项卡内容 : setContent(), 
 * 可以设置视图组件, 可以设置Activity, 也可以设置Fragement; 
 */ 
 tabHost.addTab(tabHost.newTabSpec("tabTime").setIndicator("时钟") 
  .setContent(R.id.tabTime)); 
 tabHost.addTab(tabHost.newTabSpec("tabAlarm").setIndicator("闹钟") 
  .setContent(R.id.tabAlarm)); 
 tabHost.addTab(tabHost.newTabSpec("tabTimer").setIndicator("计时器") 
  .setContent(R.id.tabTimer)); 
 tabHost.addTab(tabHost.newTabSpec("tabStopWatch").setIndicator("秒表") 
  .setContent(R.id.tabStopWatch)); 
} 

在MainActivity中主要的操作就是设置TabHost,上面的代码中已经贴上了解释,这里就不讲了,接下来一篇我们就重点来讲讲时钟、闹钟、计时器和秒表这四部分,希望大家继续学习。

以上就是本文的全部内容,希望对大家学习Android软件编程有所帮助。

相关文章

  • Android 知乎广告效果实现代码

    Android 知乎广告效果实现代码

    这篇文章主要介绍了Android 知乎广告效果实现代码,需要的朋友可以参考下
    2018-01-01
  • Ubuntu中为Android HAL编写JNI方法提供JAVA访问硬件服务接口

    Ubuntu中为Android HAL编写JNI方法提供JAVA访问硬件服务接口

    本文主要介绍Ubuntu中为Android硬件抽象层模块编写JNI方法提供Java访问硬件服务接口,这里给大家详细说明如何编写 JNI方法访问硬件接口并附示例代码,有需要的小伙伴参考下
    2016-08-08
  • Google 开发Android MVP架构Demo深入解析

    Google 开发Android MVP架构Demo深入解析

    这篇文章主要为大家介绍了Google 开发Android MVP架构Demo深入解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-11-11
  • Android仿微信键盘切换效果

    Android仿微信键盘切换效果

    这篇文章主要为大家详细介绍了Android仿微信键盘切换效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-11-11
  • Android优化方案之Fragment的懒加载实现代码

    Android优化方案之Fragment的懒加载实现代码

    本篇文章主要介绍了Android优化方案之Fragment的懒加载实现代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-03-03
  • android屏幕圆角实现方法的示例代码

    android屏幕圆角实现方法的示例代码

    本篇文章主要介绍了android屏幕圆角实现方法的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-11-11
  • android实现音乐播放器

    android实现音乐播放器

    这篇文章主要为大家详细介绍了android实现音乐播放器,拥有播放、暂停、重新播放和停止等功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-04-04
  • Android之RecyclerView实现时光轴效果示例

    Android之RecyclerView实现时光轴效果示例

    本篇文章主要介绍了Android之RecyclerView实现时光轴效果,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-02-02
  • Android Jetpack组件之ViewModel使用详解

    Android Jetpack组件之ViewModel使用详解

    Android中的ViewModel是一个可以用来存储UI相关的数据的类。ViewModel的生命周期会比创建它的Activity、Fragment的生命周期长,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧
    2023-04-04
  • android TextView属性的详细介绍 分享

    android TextView属性的详细介绍 分享

    android TextView属性的详细介绍 分享,需要的朋友可以参考一下
    2013-05-05

最新评论