Android 简单天气预报
IDE: Android studio3.1.2
界面:
activity_main.xml
activity_main2.xml
代码
AndroidMainfest.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <LinearLayout android:id="@+id/linearLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" app:layout_constraintBottom_toTopOf="parent" app:layout_constraintEnd_toStartOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/title_text" android:textSize="30sp" /> <WebView android:id="@+id/webView" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> </android.support.constraint.ConstraintLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView1" android:textSize="20sp" android:text="地区" /> <RadioGroup android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/rgSex" android:layout_weight="1" android:orientation="vertical"> <RadioButton android:id="@+id/radio_bj" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="北京" android:checked="true"/> <RadioButton android:id="@+id/radio_sh" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="上海"/> </RadioGroup> <Button android:id="@+id/submit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="查询"/> </LinearLayout> </android.support.constraint.ConstraintLayout>
activity_main2.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <LinearLayout android:id="@+id/linearLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" app:layout_constraintBottom_toTopOf="parent" app:layout_constraintEnd_toStartOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/title_text" android:textSize="30sp" /> <WebView android:id="@+id/webView" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> </android.support.constraint.ConstraintLayout>
MainActivity.java
package com.example.mark.radio_weather; import android.content.Intent; import android.os.Bundle; import android.app.Activity; import android.view.View; import android.widget.Button; import android.widget.RadioButton; public class MainActivity extends Activity { private RadioButton radio_bj; private RadioButton radio_sh; private Button submit; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //单选按钮组 radio_bj = (RadioButton)findViewById(R.id.radio_bj); radio_sh = (RadioButton)findViewById(R.id.radio_sh); findViewById(R.id.submit).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //监听器要做的事情 Intent i = new Intent(MainActivity.this, Main2Activity.class); if(radio_bj.isChecked()){ i.putExtra("data", "101010100"); startActivity(i); } if(radio_sh.isChecked()){ i.putExtra("data", "101020100"); startActivity(i); } } }); } }
MainActivity.java
package com.example.mark.radio_weather; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Button; import android.widget.TextView; public class Main2Activity extends AppCompatActivity { private WebView webView; private Button sh; private Button bj; private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); Intent i = getIntent(); //获取按钮传递数值 String getData = i.getStringExtra("data"); webView = (WebView)findViewById(R.id.webView);//获取web组件 WebSettings webSettings = webView.getSettings(); webSettings.setJavaScriptEnabled(true);//设置JavaScript可用 webView.setWebChromeClient(new WebChromeClient());//处理JavaScript对话框 //处理各种通知和请求时间,如果不使用该句代码,将使用内置浏览器访问网页 webView.setWebViewClient(new WebViewClient()); // 设置默认显示天气预报信息 webView.loadUrl("http://m.weather.com.cn/mweather/101010100.shtml"); openUrl(getData); } private void openUrl(String id){ webView.loadUrl("http://m.weather.com.cn/mweather/" + id + ".shtml"); } }