查询城市天气预报
时间:2025-07-11
时间:2025-07-11
查询城市天气预报信息主页面WeatherActivity继承android.app.Activity, 并 重写 onCreate 方法。
onCreate 方法是查询页面的初始化方法,在这个方法中我们要初始化 页面中 Button,Spinner 等控件信息。
showCity 方法,列表显示查询天气预报信息的城市名称。
updateWeatherInfoView 方法,更新显示选中城市的当前天气信息。 updateWeatherorecastView 方法,更新显示选中城市未来四天的天气 信息。
getCityWeather 方法,通过网络得到选中城市的天气预报信息。
查 询 城 市 天 气 预 报 详 细 信 息 页 面 WeatherDetailActivity 继 承 android.app.Activity, 并重写 onCreate 方法。
onCreate 方法是查询天气预报详细页面的初始化方法,在这个方法中 我们要初始化页面中 TextView 等控件信息。
showDetailInfo 方法,得到查询目标城市的天气预报的风向、能见度、 相对湿度等详细信息。
1.5 系统实现代码解析
1.5.1 Android 客户端系统配置文件
AndroidManifest.xml 是系统中的配置文件,我们需要将系统中用到的
Android 组件都在这个文件中注册,其中 WeatherActivity 是显示天气预报的主 页面,WeatherDetailActivity 是显示天气预报详细的的页面。为了访问 Google 提供天气预报信息的站点和能够访问提供选定城市更多信息的 Web Service 必 须为系统开放访问 Internet 的权限:
<uses-permission
android:name="android.permission.INTERNET"></uses-permission>。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://www.77cn.com.cn/apk/res/android"
package="com.miiceic.weather.activity"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".WeatherActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="http://www.77cn.com.cnUNCHER" />
</intent-filter>
</activity>
<activity android:name=".WeatherDetailActivity"></activity>
</application>
<uses-sdk android:minSdkVersion="3" />
<uses-permission
android:name="android.permission.INTERNET"></uses-permission>
</manifest>
代码清单 1 AndroidManifest.xml
第 12 页 共 31 页
1.5.2 Android 城市天气预报系统布局文件
城市天气预报主页面布局采用 XML 形式,文件放置在\res\layout\main.xml 中。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://www.77cn.com.cn/apk/res/android" android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/white"
>
<TextView android:id="@+id/TextView001" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/inputstr"
android:textStyle="bold" android:textSize="16px"
android:layout_marginLeft="10px"
android:textColor="@color/black"></TextView>
<TableLayout android:id="@+id/TableLayout02"
android:layout_height="wrap_content" android:layout_width="fill_parent"> <TableRow android:id="@+id/TableRow01"
android:layout_height="wrap_content" android:layout_width="fill_parent">
<TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/msg"
android:textStyle="bold" android:textSize="16px"
android:layout_marginLeft="10px"
android:textColor="@color/black"></TextView>
<Spinner android:id="@+id/Spinner01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:paddingLeft="10px"
android:minWidth="200px"></Spinner>
<Button android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/OK"
android:paddingLeft="10px"></Button>
</TableRow>
</TableLayout>
<TableLayout android:id="@+id/TableLayout01"
android:layout_width="fill_parent" android:layout_height="wrap_content"> <TableRow android:id="@+id/TableRow02"
android:layout_width="wrap_content" android:layout_height="wrap_content"> <com.miiceic.weather.view.WeatherInfoView android:id="@+id/weather_0" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </TableRow>
代码清单 2 main.xml 布局文件
第 13 页 共 31 页
<TableRow android:id="@+id/TableRow03"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<com.miiceic.weather.view.WeatherInfoView android:id="@+id/weather_1" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </TableRow>
<TableRow android:id="@+id/TableRow04"
android:layout_width="wrap_conte …… 此处隐藏:7668字,全部文档内容请下载后查看。喜欢就下载吧 ……