怎么在Android中使用Internet獲取天氣預(yù)報(bào)

這篇文章將為大家詳細(xì)講解有關(guān)怎么在Android中使用Internet獲取天氣預(yù)報(bào),文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

廣漢ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)建站的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!

Android是什么

Android是一種基于Linux內(nèi)核的自由及開放源代碼的操作系統(tǒng),主要使用于移動(dòng)設(shè)備,如智能手機(jī)和平板電腦,由美國Google公司和開放手機(jī)聯(lián)盟領(lǐng)導(dǎo)及開發(fā)。

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獲取天氣預(yù)報(bào)

要在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); 
 //處理各種通知請(qǐng)求和事件,如果不使用該句代碼,將使用內(nèi)置瀏覽器訪問網(wǎng)頁 
 webview.setWebViewClient(new WebViewClient()); 
 webview.getSettings().setJavaScriptEnabled(true);//設(shè)置兼容JavaScript 
 webview.setWebChromeClient(new WebChromeClient());//處理JavaScript對(duì)話框 
 //設(shè)置默認(rèn)顯示的天氣預(yù)報(bào)信息 
 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ù)報(bào)信息 
 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

關(guān)于怎么在Android中使用Internet獲取天氣預(yù)報(bào)就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

本文名稱:怎么在Android中使用Internet獲取天氣預(yù)報(bào)
文章地址:http://muchs.cn/article22/ijdpjc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、App開發(fā)響應(yīng)式網(wǎng)站、面包屑導(dǎo)航、網(wǎng)站收錄網(wǎng)站改版

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管