这次是一个时钟类应用,目前依旧是主要的功能,长得还是很挫。当然了,核心功能是有的……
闹钟之前的准备
布局的话,不管是采用FrameLayout或者LinearLayout都可以。
我这里采用了FrameLayout,然后加上一个TabHost,之前在论坛看到有同学提问在WF中这种多个栏目的用什么控件,我的答案是在WF、WPF、Windows App、ASP.NET以及安卓上都是Tab开头的控件。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TabHost
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tabHost">
<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">
</FrameLayout>
</LinearLayout>
</TabHost>
</FrameLayout>
定义好了TabHost之后就可以在MainAcitivity类中实例化它们了。
private static TabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost = (TabHost)findViewById(R.id.tabHost);
tabHost.setup();
tabHost.addTab(tabHost.newTabSpec("tabAlarm").
setIndicator("闹钟").setContent(R.id.tabAlarm));
tabHost.addTab(tabHost.newTabSpec("tabTime").
setIndicator("时钟").setContent(R.id.tabTime));
tabHost.addTab(tabHost.newTabSpec("tabTimer").
setIndicator("计时器").setContent(R.id.tabTimer));
tabHost.addTab(tabHost.newTabSpec("tabStopWatch").
setIndicator("秒表").setContent(R.id.tabStopWatch));
}
如果你想调整“闹钟”与“时钟”这些的先后顺序,直接在代码中调整代码执行顺序即可。
AlarmView
首先呢,别的不管,我为大家准备了非常好听的铃声,可惜铃声只能放源码里而不能放博客上……
现在来写一个AlarmView类,到时候在布局文件中可以直接使用它。
<myapplication.nomasp.com.clock.AlarmView
android:id="@+id/tabAlarm"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/lvAlarmList"
android:layout_weight="1"
android:layout_width=

本文介绍如何在Android平台上开发一个简易的时钟应用,包括闹钟功能。首先,讨论了闹钟之前的准备工作,如布局设计和TabHost的使用。接着详细讲解了AlarmView类的实现,包括闹钟管理类的实例化和核心代码展示。还介绍了PlayAlarmAty活动,用于显示闹钟激活时的界面,并提到了AlarmReceiver广播接收器的角色。最后,提到了清单文件的配置以及数据保存和加载的重要性。项目代码已上传至Github,欢迎参与贡献。
最低0.47元/天 解锁文章
487





