Android Internet应用实现获取天气预报的示例代码

作者:光仔December 时间:2023-09-26 04:13:22 

在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>

布局效果如图

Android Internet应用实现获取天气预报的示例代码

要在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

运行结果如图

Android Internet应用实现获取天气预报的示例代码

来源:https://blog.csdn.net/acmman/article/details/46509511

标签:android,天气预报
0
投稿

猜你喜欢

  • Java计时新姿势StopWatch的使用方法详解

    2022-01-28 00:49:57
  • Android 开发程序锁应用简单实例

    2021-10-07 07:41:34
  • spring boot springMVC扩展配置实现解析

    2023-11-25 10:32:53
  • java二叉树面试题详解

    2021-06-13 08:40:23
  • Mybatis逆工程jar包的修改和打包

    2023-06-03 09:28:14
  • C#实现将汉字转化为2位大写的16进制Unicode的方法

    2022-03-11 21:45:07
  • Java jpa外连接查询join案例详解

    2022-12-17 18:31:15
  • 解决Android MediaRecorder录制视频过短问题

    2023-04-24 01:47:56
  • 十分钟速懂java知识点 System类

    2022-11-25 04:19:26
  • android 调用系统的照相机和图库实例详解

    2022-01-02 11:46:47
  • mybatisplus逻辑删除基本实现和坑点解决

    2021-05-24 11:35:50
  • Java获取环境变量(System.getenv)的方法

    2021-10-06 03:23:47
  • java 实现定时的方法及实例代码

    2023-03-31 22:48:41
  • eclipse的git插件安装、配置与使用详解

    2021-07-23 10:04:47
  • Android如何从实现到封装一个MVP详解

    2023-02-12 10:44:40
  • java io读取文件操作代码实例

    2023-04-12 08:53:57
  • SpringCloud学习笔记之OpenFeign进行服务调用

    2021-05-25 12:32:58
  • 基于Spring Security的Oauth2授权实现方法

    2022-12-19 16:52:18
  • C# IsDefined的问题

    2022-07-15 10:37:13
  • Android自定义View仿大众点评星星评分控件

    2023-07-22 22:37:28
  • asp之家 软件编程 m.aspxhome.com