最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android Internet應(yīng)用實現(xiàn)獲取天氣預(yù)報的示例代碼

 更新時間:2018年05月03日 10:01:37   作者:光仔December  
這篇文章主要介紹了Android網(wǎng)絡(luò)編程及Internet應(yīng)用-獲取天氣,小編覺得挺不錯的,一起跟隨小編過來看看吧

在Eclipse中創(chuàng)建Android項目,利用之前學(xué)過的WebView控件和中國天氣網(wǎng)提供的天氣數(shù)據(jù)接口,實現(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è)置強制橫屏(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 

運行結(jié)果如圖

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

  • Android開發(fā)實現(xiàn)Gallery畫廊效果的方法

    Android開發(fā)實現(xiàn)Gallery畫廊效果的方法

    這篇文章主要介紹了Android開發(fā)實現(xiàn)Gallery畫廊效果的方法,結(jié)合具體實例形式分析了Android使用Gallery實現(xiàn)畫廊功能的具體操作技巧與相關(guān)注意事項,需要的朋友可以參考下
    2017-06-06
  • Android天氣預(yù)報app改進版

    Android天氣預(yù)報app改進版

    這篇文章主要為大家詳細介紹了改進版的Android天氣預(yù)報app,內(nèi)容更加充實,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-07-07
  • Android?十六進制狀態(tài)管理實例詳解

    Android?十六進制狀態(tài)管理實例詳解

    這篇文章主要為大家介紹了Android?十六進制狀態(tài)管理實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-09-09
  • Android自帶API實現(xiàn)分享功能

    Android自帶API實現(xiàn)分享功能

    這篇文章主要為大家詳細介紹了Android自帶API實現(xiàn)分享功能,實現(xiàn)文字和圖片的分享,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • 解決android Listview的item中最外層Margin失效的問題

    解決android Listview的item中最外層Margin失效的問題

    下面小編就為大家?guī)硪黄鉀Qandroid Listview的item中最外層Margin失效的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • Retrofit2日志攔截器的使用

    Retrofit2日志攔截器的使用

    這篇文章主要介紹了Retrofit2日志攔截器的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-11-11
  • 詳解Flutter如何繪制曲線,折線圖及波浪動效

    詳解Flutter如何繪制曲線,折線圖及波浪動效

    這篇文章主要為大家介紹線條類圖形的繪制(正弦曲線、折線圖),并且結(jié)合 Animation 實現(xiàn)了常見的波浪動效,感興趣的小伙伴可以了解一下
    2022-03-03
  • Android自定義View實現(xiàn)圓形加載進度條

    Android自定義View實現(xiàn)圓形加載進度條

    這篇文章主要為大家詳細介紹了Android自定義View實現(xiàn)圓形加載進度條,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • Android 實現(xiàn)自定義圓形進度條的實例代碼

    Android 實現(xiàn)自定義圓形進度條的實例代碼

    進度條在Android中教程使用到,本文章向大家介紹一下Android自定義圓形進度條實現(xiàn)代碼,需要的朋友可以參考一下。
    2016-11-11
  • Android網(wǎng)絡(luò)通信的實現(xiàn)方式

    Android網(wǎng)絡(luò)通信的實現(xiàn)方式

    這篇文章主要為大家詳細介紹了Android網(wǎng)絡(luò)通信的實現(xiàn)方式,四種實現(xiàn)網(wǎng)絡(luò)通信的方式供大家學(xué)習(xí),感興趣的小伙伴們可以參考一下
    2016-06-06

最新評論

潼关县| 延寿县| 马边| 壶关县| 怀仁县| 威远县| 通许县| 田东县| 阿图什市| 民和| 农安县| 喀什市| 凌海市| 苍南县| 石棉县| 勃利县| 武定县| 喀喇沁旗| 揭阳市| 大宁县| 商河县| 始兴县| 潼南县| 美姑县| 迁西县| 嘉定区| 新巴尔虎左旗| 抚顺县| 马龙县| 习水县| 获嘉县| 苍山县| 华容县| 余江县| 南昌县| 三门县| 榆树市| 云龙县| 大丰市| 大城县| 民勤县|