Android 获取天气预报

界面布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >

        <Button
            android:id="@+id/bj"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/bj" />

        <Button
            android:id="@+id/sh"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/sh" />

        <Button
            android:id="@+id/heb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/heb" />

        <Button
            android:id="@+id/cc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/cc" />

        <Button
            android:id="@+id/sy"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/sy" />

        <Button
            android:id="@+id/gz"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/gz" />

    </LinearLayout>
    <WebView android:id="@+id/webView1"
        android:layout_width="wrap_content"
        android:layout_height="0dip"
        android:focusable="false"
        android:layout_weight="1"
        />

</LinearLayout>

后台代码

package com.basillee.asus.demo;
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 MainActivity7 extends Activity implements OnClickListener {
    private WebView webView;    //声明WebView组件的对象

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_activity7);
        webView=(WebView)findViewById(R.id.webView1);    //获取WebView组件
        webView.getSettings().setJavaScriptEnabled(true);    //设置JavaScript可用f
        webView.setWebChromeClient(new WebChromeClient());    //处理JavaScript对话框
        webView.setWebViewClient(new WebViewClient());    //处理各种通知和请求事件,如果不使用该句代码,将使用内置浏览器访问网页
        webView.loadUrl("http://m.weather.com.cn/m/pn12/weather.htm ");    //设置默认显示的天气预报信息
        webView.setInitialScale(57*4);    //放网页内容放大4倍
        Button bj=(Button)findViewById(R.id.bj);        //获取布局管理器中添加的“北京”按钮
        bj.setOnClickListener(this);
        Button sh=(Button)findViewById(R.id.sh);        //获取布局管理器中添加的“上海”按钮
        sh.setOnClickListener(this);
        Button heb=(Button)findViewById(R.id.heb);        //获取布局管理器中添加的“哈尔滨”按钮
        heb.setOnClickListener(this);
        Button cc=(Button)findViewById(R.id.cc);        //获取布局管理器中添加的“长春”按钮
        cc.setOnClickListener(this);
        Button sy=(Button)findViewById(R.id.sy);        //获取布局管理器中添加的“沈阳”按钮
        sy.setOnClickListener(this);
        Button gz=(Button)findViewById(R.id.gz);        //获取布局管理器中添加的“广州”按钮
        gz.setOnClickListener(this);
    }
    @Override
    public void onClick(View view){
        switch(view.getId()){
            case R.id.bj:        //单击的是“北京”按钮
                openUrl("101010100T");
                break;
            case R.id.sh:        //单击的是“上海”按钮
                openUrl("101020100T");
                break;
            case R.id.heb:        //单击的是“哈尔滨”按钮
                openUrl("101050101T");
                break;
            case R.id.cc:        //单击的是“长春”按钮
                openUrl("101060101T");
                break;
            case R.id.sy:        //单击的是“沈阳”按钮
                openUrl("101070101T");
                break;
            case R.id.gz:        //单击的是“广州”按钮
                openUrl("101280101T");
                break;
        }
    }
    //打开网页的方法
    private void openUrl(String id){
        webView.loadUrl("http://m.weather.com.cn/m/pn12/weather.htm?id="+id+" ");    //获取并显示天气预报信息
    }
}

更多信息: http://jingyan.baidu.com/season/48891

时间: 2024-08-16 17:11:52

Android 获取天气预报的相关文章

Android实战--天气预报(API+JSON解析)

学习安卓有一段时间了,应该提高自己的实战能力,做一些简单的Demo.下面我们介绍一下如何利用网络API实现天气预报功能,主要涉及到如何利用API获得网络数据,网络数据返回一般是JSON格式,这里又涉及到JSON的解析问题,这些都是比较基础的问题,应该予以掌握. 首先在http://apistore.baidu.com/?qq-pf-to=pcqq.c2c找到你想要的API,这里我们选择http://apistore.baidu.com/astore/serviceinfo/1798.html,网

基于android的天气预报的设计与实现

目录 应用开发技术及开发平台介绍 应用需求分析 应用功能设计及其描述 应用UI展示 ①开发技术: 本系统是采用面向对象的软件开发方法,基于Android studio开发平台,以Android作为本系统的开发语言实现音乐播放器预定的需求功能. ②平台介绍 硬件平台 CPU奔腾双核 (主频2.0GHz) 内存1G以上 64或32位PC机 500G硬盘 软件平台 操作系统:Windows XP \ Win7\Win8\win10 开发工具:Android Studio 本软件占用系统空间小,能满足用

android 获取相册列表的实现(三)

该项目实现的功能如下: 获取手机相册,点击每个相册之后进入该相册的图片列表界面,在图片列表界面可以实现图片多选,然后进入所选择的图片界面,在该界面内可以实现所选图片的上传等功能. 该项目最大特色: 1.获取相册列表,目前网络上面介绍获取相册的项目很少,本文专门讲述相册的获取. 2.使用Android-Universal-Image-Loader集成框架-第三方jar包加载本地图片,熟悉这个jar的开发者肯定不陌生,该jar包十分强大,除了可以获取网络图片,本地图片也是可以的.同时,通过引用第三方

android获取某应用的帧数据

android获取某应用的帧数据: 说明: (打开手机 开发者选项-GPU显示配置文件(显示于 adb shell dumpsys gfxinfo)勾上,只有4.1以上支持) 在android4.1中,谷歌提供了一个工具来,叫做" GPU呈现模式分析(Profile GPU rendering)", 在开启这个功能后,系统就会记录保留每个界面最后128帧图像绘制的相关时间信息 开始操作: 命令行 adb shell dumpsys gfxinfo com.taobao.mobile.d

android获取图片的旋转角度

public static int getExifOrientation(String filepath) { int degree = 0; ExifInterface exif = null; try { exif = new ExifInterface(filepath); } catch (IOException ex) { Log.d(TAG, "cannot read exif" + ex); } if (exif != null) { int orientation =

java获取天气预报数据

获取天气预报数据 对于做web项目有天气数据的需求,这个服务很合适: WebXml.com.cn 2400多个城市天气预报Web服务,包含2300个以上中国城市和100个以上国外城市天气预报数据.数据每2.5小时左右自动更新一次,准确可靠.提供webservice 接口,主连接:http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/ 方法调用说明如下: (1)getRegionCountry :获得国外国家名称和与之对应的ID 说明

Android获取设备采用的时间制式(12小时制式或24小时制式)

/** * 获取设备采用的时间制式(12小时制式或者24小时制式) * 注意: * 在模拟器上获取的时间制式为空 */ private void getTime_12_24(Context context){ ContentResolver contentResolver = context.getContentResolver(); String time_12_24 = Settings.System.getString(contentResolver,Settings.System.TIM

Android获取LayoutInflater对象的方法总结

在写Android程序时,有时候会编写自定义的View,使用Inflater对象来将布局文件解析成一个View.本文主要目的是总结获取LayoutInflater对象的方法. 1.若能获取context对象,可以有以下几种方法: LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View child = inflater.inflate(R.la

Android 获取剩余存储空间

获取android 设备可用的本地存储空间剩余大小 File datapath = Environment.getDataDirectory();StatFs  dataFs=new StatFs(datapath.getPath()); long sizes=(long)dataFs.getFreeBlocks()*(long)dataFs.getBlockSize();long available=sizes/((1024*1024)); Android 获取剩余存储空间,布布扣,bubuk