android studio中 实战演练——天气预报 程序

实战演练——天气预报 程序

(有问题可以评论,一起交流)
1.创建程序
android studio中 实战演练——天气预报 程序_第1张图片
android studio中 实战演练——天气预报 程序_第2张图片
2.创建完成之后,设计用户交互界面。
编辑activity_main.xml 文件:(代码如下)




    
    
    
    
        
        
        
    
    
    
        

2.创建一个文件夹raw(在res里面创建)
android studio中 实战演练——天气预报 程序_第3张图片
3.raw文件夹创建成功之后,在里面创建weather1.xml 文件(这个文件中包含天气的一些信息)
weather.xml 代码如下:



    
        20`C/30`C
        晴转多云
        上海
        80
        1级
    
    
        26`C/32`C
        
        北京
        98
        3级
    
    
        15`C/24`C
        多云
        广州
        30
        5级
    

4.创建WeatherInfo类(为了方便使用weather.xml中的属性,封装成一个类),创建WeatherInfo类,具体代码如下:

package com.example.weather;

public class WeatherInfo {
    private String id;
    private String temp;
    private String weather;
    private String name;
    private String pm;
    private String wind;
    public String getId(){
        return id;
    }
    public void setId(String id){
        this.id=id;
    }
    public String getTemp(){
        return temp;
    }
    public void setTemp(String temp){
        this.temp=temp;
    }
    public String getWeather(){
       return weather;
    }
    public void setWeather(String weather){
        this.weather=weather;
    }
    public String getName(){
        return name;
    }
    public void setName(String name){
        this.name=name;
    }
    public String getPm(){
        return pm;
    }
    public void setPm(String pm){
        this.pm=pm;
    }
    public String getWind(){
        return wind;
    }
    public void setWind(String wind){
        this.wind=wind;
    }
}

5.创建WeatherService 工具类(为了代码更加易于阅读,避免大量代码都在一个类中,因此创建一个用来解析XML文件的工具类WeatherService)。
具体代码如下:

package com.example.weather;
import android.util.Xml;
import org.xmlpull.v1.XmlPullParser;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class WeatherService {
    public static List getInfsFromXML (InputStream is) throws Exception{
        XmlPullParser parser= Xml.newPullParser();
        parser.setInput(is,"utf-8");
        List weatherInfos=null;
        WeatherInfo weatherInfo =null;
        int type =parser.getEventType();
        while (type != XmlPullParser.END_DOCUMENT){
            switch (type){
                case XmlPullParser.START_TAG:
                    if ("infos".equals(parser.getName())){
                        weatherInfos= new ArrayList();
                    }else if ("city".equals(parser.getName())){
                        weatherInfo =new WeatherInfo();
                        String idStr =parser.getAttributeValue(0);
                        weatherInfo.setId(idStr);
                    }else if ("temp".equals(parser.getName())){
                        String temp=parser.nextText();
                        weatherInfo.setTemp(temp);
                    }else if ("weather".equals(parser.getName())){
                        String weather =parser.nextText();
                        weatherInfo.setWeather(weather);
                    }else if ("name".equals(parser.getName())){
                        String name =parser.nextText();
                        weatherInfo.setName(name);
                    }else if ("pm".equals(parser.getName())){
                        String pm=parser.nextText();
                        weatherInfo.setPm(pm);
                    }else if ("wind".equals(parser.getName())){
                        String wind =parser.nextText();
                        weatherInfo.setWind(wind);
                    }
                    break;
                case XmlPullParser.END_TAG:
                    if ("city".equals(parser.getName())){
                        weatherInfos.add(weatherInfo);
                        weatherInfo=null;
                    }
                    break;
            }
            type=parser.next();
        }
        return weatherInfos;
    }
}

6.编写界面交互代码(MainActivity.java 中,需要将解析到的 weather1.xml 文件中的数据展示在文本控件中)。具体代码如下:

package com.example.weather;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private TextView tvCity;
    private TextView tvWeather;
    private TextView tvTemp;
    private TextView tvWind;
    private TextView tvpm;
    private ImageView ivIcon;
    private Map map;
    private List> list;
    private String temp,weather,name,pm,wind;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        intView();
        try {
            InputStream is=this.getResources().openRawResource(R.raw.weather1);
            List weatherInfos=WeatherService.getInfsFromXML(is);
            list =new ArrayList>();
            for (WeatherInfo info:weatherInfos){
                map=new HashMap();
                map.put("temp",info.getTemp());
                map.put("weather",info.getWeather());
                map.put("name",info.getName());
                map.put("pm",info.getPm());
                map.put("wind",info.getWind());
                list.add(map);
            }
        } catch (Exception e){
            e.printStackTrace();
            Toast.makeText(this,"解析信息失败",Toast.LENGTH_SHORT).show();
        }
        getMap(1,R.drawable.sun);
    }
    private void intView(){
        tvCity =(TextView) findViewById(R.id.tv_city);
        tvWeather=(TextView) findViewById(R.id.tv_weather);
        tvTemp=(TextView) findViewById(R.id.tv_temp);
        tvWind=(TextView) findViewById(R.id.tv_wind);
        tvpm=(TextView) findViewById(R.id.tv_pm);
        ivIcon =(ImageView) findViewById(R.id.iv_icon);
        findViewById(R.id.btn_sh).setOnClickListener(this);
        findViewById(R.id.btn_gz).setOnClickListener(this);
        findViewById(R.id.btn_bj).setOnClickListener(this);
    }
    public void onClick(View v){
        switch (v.getId()){
            case R.id.btn_sh:
                getMap(0,R.drawable.cloud_sun1);
                break;
            case R.id.btn_bj:
                getMap(1,R.drawable.sun);
                break;
            case R.id.btn_gz:
                getMap(2,R.drawable.clouds);
                break;
        }
    }
    private void getMap(int number,int iconNumber){
        Map cityMap=list.get(number);
        temp=cityMap.get("temp");
        weather=cityMap.get("weather");
        name=cityMap.get("name");
        pm=cityMap.get("pm");
        wind=cityMap.get("wind");
        tvCity.setText(name);
        tvWeather.setText(weather);
        tvTemp.setText(""+temp);
        tvWind.setText("风力:"+wind);
        tvpm.setText("pm:"+pm);
        ivIcon.setImageResource(iconNumber);
    }
}

7.运行程序(当 运行程序时,单机“北京”,“上海”,“广州”按钮,即能够展示不同的城市的天气信息),我做的运行程序如下:
android studio中 实战演练——天气预报 程序_第4张图片
总结:
下面这个是我做的实战演练。
注意:在做的时候我们需要用到一些图片,图片的话 我们自己去网上搜,自己喜欢就好。下载下来,然后复制粘贴到我们的Android studio 里面,直接复制粘贴就好了。但是我们做的时候要注意一些 图片名称的命名,不然会报错。如果有什么不懂,可以相互交流,私信或者评论,我看到消息会及时回复,我也是刚起步,一起学习!
android studio中 实战演练——天气预报 程序_第5张图片

你可能感兴趣的:(Android,studio,Java,安卓,android,studio)