Android Internet應(yīng)用實(shí)現(xiàn)獲取天氣預(yù)報的示例代碼
在Eclipse中創(chuàng)建Android項(xiàng)目,利用之前學(xué)過的WebView控件和中國天氣網(wǎng)提供的天氣數(shù)據(jù)接口,實(shí)現(xiàn)獲取指定城市的天氣預(yù)報。
布局文件:
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中設(shè)置強(qiáng)制橫屏(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); //處理各種通知請求和事件,如果不使用該句代碼,將使用內(nèi)置瀏覽器訪問網(wǎng)頁 webview.setWebViewClient(new WebViewClient()); webview.getSettings().setJavaScriptEnabled(true);//設(shè)置兼容JavaScript webview.setWebChromeClient(new WebChromeClient());//處理JavaScript對話框 //設(shè)置默認(rèn)顯示的天氣預(yù)報信息 webview.loadUrl("http://m.weather.com.cn/m/pn12/weather.htm"); webview.setInitialScale(57*4);//將網(wǎng)頁內(nèi)容放大四倍 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) { //獲取并顯示天氣預(yù)報信息 webview.loadUrl("http://m.weather.com.cn/m/pn12/weather.htm?id="+id+""); } }
別忘記在AndroidManifest.xml中加入訪問網(wǎng)絡(luò)的權(quán)限:
<!-- 添加鏈接網(wǎng)絡(luò)的權(quán)限 --> uses-permissionandroid:name="android.permission.INTERNET
運(yùn)行結(jié)果如圖
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
Android?Flutter實(shí)現(xiàn)有趣的頁面滾動效果
Flutter提供了?CustomScrollView?來粘合多個滑動組件,并且可以實(shí)現(xiàn)更有趣的滑動效果,本文就來為大家詳細(xì)講講實(shí)現(xiàn)的方法,需要的可以參考一下2022-06-06android中實(shí)現(xiàn)指針滑動的動態(tài)效果方法
本次實(shí)現(xiàn)的是類似于墨跡天氣中軌跡圖片上指針隨著數(shù)值滾動滑動的效果,基本思路是開啟線程,控制指針?biāo)诘膇mageview控件的padding屬性。2013-03-03Android軟鍵盤的顯示隱藏功能實(shí)現(xiàn)過程
這篇文章主要介紹了Android軟鍵盤的顯示隱藏功能,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-03-03Retrofit自定義請求參數(shù)注解的實(shí)現(xiàn)思路
這篇文章主要給大家介紹了Retrofit自定義請求參數(shù)注解的實(shí)現(xiàn)思路,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12詳解Android平臺JSON預(yù)覽(JSON-handle)
這篇文章主要介紹了Android平臺JSON預(yù)覽(JSON-handle),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-09Android實(shí)現(xiàn)循環(huán)平移動畫示例
這篇文章主要介紹了Android實(shí)現(xiàn)循環(huán)平移動畫示例,本文講解實(shí)現(xiàn)用一張背景圖做循環(huán)從左往右平移動畫,需要的朋友可以參考下2015-06-06