Android Internet应用实现获取天气预报的示例代码
作者:光仔December 发布时间:2023-09-26 04:13:22
标签:android,天气预报
在Eclipse中创建Android项目,利用之前学过的WebView控件和中国天气网提供的天气数据接口,实现获取指定城市的天气预报。
布局文件:
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);
}
@Override
public 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-permissionandroid:name="android.permission.INTERNET
运行结果如图
来源:https://blog.csdn.net/acmman/article/details/46509511


猜你喜欢
- Android Java 如何调用自己的 C++ 的类库下面以 Java 调用 C++ 的加法运算函数为例,做简单说明。(使用 Androi
- Android Studio Intent隐式启动,发短信,拨号,打电话,访问网页等实例代码功能创建5个按钮,隐式启动、发短信、拨号按钮、电
- 接触了这么久的View,总不能一直停留在View里,现在开始呢,就要学习一个新的知识点:SurfaceView,实际上SurfaceView
- 今天要介绍一个概念,对象的克隆。本篇有一定难度,请先做好心理准备。看不懂的话可以多看两遍,还是不懂的话,可以在下方留言,我会看情况进行修改和
- 整理文档,搜刮出一个android 通过MediaRecorder实现简单的录音示例,稍微整理精简一下做下分享。MainActivitypa
- 问题我们发现,之所以我们现在离不开 xml 配置文件,是因为我们有一句很关键的配置:<!-- 告知spring框架在,读取配置文件,创
- 上文对数据结构与算法,有了一个简单的概述与介绍,这篇文章,我们介绍一中典型数据结构——线性结构。什么是线性结构,线性结构是最简单、最基本、最
- 前言关系复杂度一、直接插入排序基本思想:将新的数据插入已经排好的数据列中。将第一个和第二个数排序,构成有序数列然后将第三个数插进去,构成新的
- 排列组合是常见的数学问题,本文就以完整实例形式讲述了C#实现排列组合算法的方法。分享给大家供大家参考之用。具体方法如下:首先,数学中排列组合
- 一、引言在移动应用程序的架构设计中,界面与数据即不可分割又不可混淆。在绝大部分的开发经历中,我们都是使用Fragment来进行界面编程,即使
- 本文实例讲述了Android自动朗读TTS用法。分享给大家供大家参考,具体如下:TextToSpeech简称 TTS,是自Android 1
- 前面照着android系统的裁剪图片的功能自己写了一个相似的工具。功能是大体上实现了,但留下了一个调用的问题:如何从我的程序调用这个裁剪工具
- 本文实例讲述了Android编程处理窗口控件大小,形状,像素等UI元素工具类。分享给大家供大家参考,具体如下:/*** 处理窗口控件大小,形
- 目录前言应用定义基本Enum特性Enum的静态导入Enum中添加新方法Switch语句中的EnumEnum的继承EnumSet的使用Enum
- 网上教程7.0大多数配置是这样compile ‘com.jakewharton:butterknife:7.0.1' ,不知道他们用
- 1.构建springboot项目2.打包应用3.编写dockerfile4.构建镜像5.发布运行![root@localhost demo]
- Spring Security 过滤器链及自定义Filter别名类名称Namespace Element or AttributeCHANN
- 在装2个不同版本JDK时遇到了这个问题,在网上钩了一吧!查到一个讲解比较好的资料。一:要解决的问题我们在尝鲜 JDK1.5 的时候,相信不少
- 一、Map接口继承树Map:双列数据,存储key-value对的数据 ---类似于高中的函数:y = f(x)A.HashMap:作为Map
- 前言本文主要给大家介绍了关于Android模仿美团顶部滑动菜单的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。先