Android 简单天气预报

IDE: Android studio3.1.2

 

界面:

activity_main.xml

activity_main2.xml

 

代码

 

AndroidMainfest.xml





    

        


        

    


 

activity_main.xml




    
        
        
            
            

        
        

 activity_main2.xml





    

        


        

    


 

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");
    }

}

 

你可能感兴趣的:(Android 简单天气预报)