【Android开发】网络编程及Internet应用-获取天气预报
16lz
2021-01-26
在Eclipse中创建Android项目,利用之前学过的WebView控件和中国天气网提供的天气数据接口,实现获取指定城市的天气预报。
布局文件:
res/layout/main.xml:
要在AndroidManifest.xml中设置强制横屏(android:screenOrientation="landscape"):
MainActivity:
别忘记在AndroidManifest.xml中加入访问网络的权限:
布局文件:
res/layout/main.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/ll1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="4" android:gravity="center" android:orientation="horizontal" > <Button android:id="@+id/beijing" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="北京"/> <Button android:id="@+id/shanghai" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="上海"/> <Button android:id="@+id/haerbin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="哈尔滨"/> <Button android:id="@+id/changchun" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="长春"/> <Button android:id="@+id/shenyang" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="沈阳"/> <Button android:id="@+id/guangzhou" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="广州"/> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:orientation="horizontal" > <WebView android:id="@+id/webview1" android:layout_width="match_parent" android:layout_height="match_parent"/></LinearLayout></LinearLayout>
布局效果如图
要在AndroidManifest.xml中设置强制横屏(android:screenOrientation="landscape"):
<activity android:name=".MainActivity" android:screenOrientation="landscape" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter></activity>
MainActivity:
package com.example.test; import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.webkit.WebChromeClient;import android.webkit.WebView;import android.webkit.WebViewClient;import android.widget.Button; public class MainActivity extends Activity implements OnClickListener{ private WebView webview; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); webview=(WebView)findViewById(R.id.webview1); //处理各种通知请求和事件,如果不使用该句代码,将使用内置浏览器访问网页 webview.setWebViewClient(new WebViewClient()); webview.getSettings().setJavaScriptEnabled(true);//设置兼容JavaScript webview.setWebChromeClient(new WebChromeClient());//处理JavaScript对话框 //设置默认显示的天气预报信息 webview.loadUrl("http://m.weather.com.cn/m/pn12/weather.htm"); webview.setInitialScale(57*4);//将网页内容放大四倍 Button bj=(Button)findViewById(R.id.beijing); bj.setOnClickListener(this); Button sh=(Button)findViewById(R.id.shanghai); sh.setOnClickListener(this); Button hrb=(Button)findViewById(R.id.haerbin); hrb.setOnClickListener(this); Button cc=(Button)findViewById(R.id.changchun); cc.setOnClickListener(this); Button sy=(Button)findViewById(R.id.shenyang); sy.setOnClickListener(this); Button gz=(Button)findViewById(R.id.guangzhou); gz.setOnClickListener(this); } @Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.beijing: //单击的是"北京"按钮openUrl("101010100T");break;case R.id.shanghai: //单击的是"上海"按钮openUrl("101020100T");break;case R.id.haerbin: //单击的是"哈尔滨"按钮openUrl("101050101T");break;case R.id.changchun: //单击的是"长春"按钮openUrl("101060101T");break;case R.id.shenyang: //单击的是"沈阳"按钮openUrl("101070101T");break;case R.id.guangzhou: //单击的是"广州"按钮openUrl("101280101T");break;default:break;}}private void openUrl(String id) {//获取并显示天气预报信息webview.loadUrl("http://m.weather.com.cn/m/pn12/weather.htm?id="+id+"");} }
别忘记在AndroidManifest.xml中加入访问网络的权限:
<!-- 添加链接网络的权限 --><uses-permission android:name="android.permission.INTERNET"/>
运行结果如图
转载请注明出处:http://blog.csdn.net/acmman/article/details/46509511
更多相关文章
- arcgis for android 学习 - (4) 了解mapView的一些方法和事件
- 安卓按钮有按下去的效果的实现方法
- UI篇--布局问题
- Android(安卓)BLE蓝牙开发中读取数据时设置Notify的方法
- android中的自定义popupwindow
- 对Menu Button 说再见
- 多种方式实现Android页面布局的切换
- android 按钮点击更改背景色或背景图
- 使用镜像站同步android sdk