本文我们进入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的一个类(讲解可以参考这里http://www.phpstudy.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软件编程有所帮助。
本文地址:https://www.stayed.cn/item/713
转载请注明出处。
本站部分内容来源于网络,如侵犯到您的权益,请 联系我