(1)activity_main.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity">
<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/yt"
android:text="@string/yt"
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"/>
</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"/>
<Button
android:id="@+id/heb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/heb"
android:textSize="30dp" />
</LinearLayout>
(2)MainActivity.java代码如下:
package com.example.weatherforecast;
import android.os.Bundle;
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.yt);
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.yt:
openUrl("101120501");
break;
case R.id.sh:
openUrl("101120502");
break;
case R.id.heb:
openUrl("101120505");
break;
case R.id.cc:
openUrl("101120505");
break;
case R.id.sy:
openUrl("101120506");
break;
case R.id.gz:
openUrl("101120507");
break;
}
}
private void openUrl(String id){
webView.loadUrl("http://m.weather.com.cn/mweather/"+id+".shtml");
}
}
(3)打开maniffests下的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:usesCleartextTraffic="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>
(4)在res中打开values文件夹下的strings.xml:代码如下
<resources>
<string name="app_name">weatherforecast</string>
<string name="GO">go</string>
<string name="yt">烟台</string>
<string name="sh">莱州</string>
<string name="gz">栖霞</string>
<string name="heb">蓬莱</string>
<string name="cc">龙口</string>
<string name="sy">招远</string>
</resources>
(5)运行结果如下