android开发【四】简单实现天气预报

时间:2024-04-09 12:37:56

android开发【四】简单实现天气预报

xml:

<?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:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/bj"
            android:text="@string/bj"
            android:textSize="30dp"/>

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

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

    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

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

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

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

    </LinearLayout>

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

    </LinearLayout>

java:

package com.example.weatherforecast;

import android.app.Activity;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
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
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webView=(WebView)findViewById(R.id.webView1);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebChromeClient(new WebChromeClient());
        webView.setWebViewClient(new WebViewClient());
        webView.loadUrl("http://www.weather.com.cn");

        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("101010100");
                break;

            case R.id.sh:
                openUrl("101020100");
                break;

            case R.id.heb:
                openUrl("101050101");
                break;

            case R.id.cc:
                openUrl("101060101");
                break;

            case R.id.sy:
                openUrl("101070101");
                break;

            case R.id.gz:
                openUrl("101280101");
                break;
        }
    }

    private void openUrl(String id){
        webView.loadUrl("http://m.weather.com.cn/mweather/"+id+".shtml");
    }

}

string变量:

<resources>
    <string name="app_name">Weather Forecast</string>
    <string name="GO">go</string>
    <string name="bj">北京</string>
    <string name="sh">上海</string>
    <string name="gz">广州</string>
    <string name="heb">哈尔滨</string>
    <string name="cc">长春</string>
    <string name="sy">沈阳</string>
</resources>

AndroidManifest.xml: 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.weatherforecast">
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>/*这里获取联网权限*/
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>