Android简单实现天气预报App

本文实例为大家分享了Android简单实现天气预报App的具体代码,供大家参考,具体内容如下

一、UI设计

首页UI

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
    android:background="@drawable/week10_3"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/text1"
        android:layout_width="133dp"
        android:layout_height="81dp"
        android:text="确定"
        android:textSize="30dp"
        android:fontFamily="sans-serif-black"
        android:gravity="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/address"
        android:layout_width="214dp"
        android:layout_height="98dp"
        android:layout_marginTop="180dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="请选择城市"
        android:gravity="center"
        android:textSize="30dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

效果:

展示页面UI

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
    android:background="@drawable/view"
    tools:context=".ShowActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="107dp"
        android:layout_height="59dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="36dp"
        android:gravity="center"
        android:text="地址"
        android:textColor="@color/white"
        android:textSize="30dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="153dp"
        android:layout_height="142dp"
        android:fontFamily="sans-serif-black"
        android:gravity="center"
        android:text="气温"
        android:textColor="@color/white"
        android:textSize="70dp"
        app:layout_constraintStart_toStartOf="@+id/textView"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="98dp"
        android:layout_height="55dp"
        android:fontFamily="sans-serif-black"
        android:gravity="center"
        android:text="天气"
        android:textColor="@color/white"
        android:textSize="30dp"
        app:layout_constraintBottom_toBottomOf="@+id/textView3"
        app:layout_constraintStart_toEndOf="@+id/textView3" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="251dp"
        android:layout_height="60dp"
        android:fontFamily="sans-serif-black"
        android:gravity="left"
        android:text="风力风向 湿度"
        android:textColor="@color/white"
        android:textSize="23dp"

        app:layout_constraintStart_toStartOf="@+id/textView3"
        app:layout_constraintTop_toBottomOf="@+id/textView3" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="97dp"
        android:layout_height="44dp"
        android:text="空气质量"
        android:textColor="@color/white"
        android:gravity="center"
        android:textSize="20dp"
        android:fontFamily="sans-serif-black"
        app:layout_constraintBottom_toTopOf="@+id/textView4"
        app:layout_constraintEnd_toEndOf="parent" />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="185dp"
        android:layout_height="190dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <TextView
            android:id="@+id/texttoday"
            android:layout_width="70dp"
            android:layout_height="45dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="20dp"
            android:fontFamily="sans-serif-black"
            android:gravity="center"
            android:text="今天"
            android:textColor="@color/white"
            android:textSize="24dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/weathertoday"
            android:layout_width="100dp"
            android:layout_height="45dp"
            android:fontFamily="sans-serif-black"
            android:gravity="center"
            android:text="TextView"
            android:textColor="@color/white"
            android:textSize="24dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="@+id/texttoday" />

        <TextView
            android:id="@+id/temtoday"
            android:layout_width="100dp"
            android:layout_height="45dp"
            android:layout_marginTop="30dp"
            android:fontFamily="sans-serif-black"
            android:gravity="left"
            android:text="TextView"
            android:textColor="@color/white"
            android:textSize="24dp"
            app:layout_constraintStart_toStartOf="@+id/texttoday"
            app:layout_constraintTop_toBottomOf="@+id/texttoday" />

        <TextView
            android:id="@+id/qualtoday"
            android:layout_width="50dp"
            android:layout_height="45dp"
            android:fontFamily="sans-serif-black"
            android:gravity="left"
            android:text="良"
            android:textColor="@color/white"
            android:textSize="24dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="@+id/temtoday" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="185dp"
        android:layout_height="190dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent">

        <TextView
            android:id="@+id/texttomo"
            android:layout_width="70dp"
            android:layout_height="45dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="20dp"
            android:text="明天"
            android:textSize="24dp"
            android:textColor="@color/white"
            android:gravity="center"
            android:fontFamily="sans-serif-black"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/weathertomo"
            android:layout_width="100dp"
            android:layout_height="45dp"
            android:layout_marginEnd="15dp"
            android:fontFamily="sans-serif-black"
            android:gravity="center"
            android:text="TextView"
            android:textColor="@color/white"
            android:textSize="24dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="@+id/texttomo" />

        <TextView
            android:id="@+id/temtomo"
            android:layout_width="100dp"
            android:layout_height="45dp"
            android:layout_marginTop="30dp"
            android:text="TextView"
            android:textSize="24dp"
            android:textColor="@color/white"
            android:gravity="center"
            android:fontFamily="sans-serif-black"
            app:layout_constraintStart_toStartOf="@+id/texttomo"
            app:layout_constraintTop_toBottomOf="@+id/texttomo" />

        <TextView
            android:id="@+id/qualtomo"
            android:layout_width="50dp"
            android:layout_height="45dp"
            android:text="TextView"
            android:textSize="24dp"
            android:textColor="@color/white"
            android:gravity="center"
            android:fontFamily="sans-serif-black"
            app:layout_constraintEnd_toEndOf="@+id/weathertomo"
            app:layout_constraintTop_toTopOf="@+id/temtomo" />
    </androidx.constraintlayout.widget.ConstraintLayout>

    <TextView
        android:id="@+id/textView16"
        android:layout_width="251dp"
        android:layout_height="60dp"
        android:fontFamily="sans-serif-black"
        android:gravity="left"
        android:text="TextView"
        android:textColor="@color/white"
        android:textSize="23dp"
        app:layout_constraintStart_toStartOf="@+id/textView3"
        app:layout_constraintTop_toBottomOf="@+id/textView5" />
</androidx.constraintlayout.widget.ConstraintLayout>

效果:

二、调用数据并上传至前台

这里我将首页设置一个输入框,里面输入地点,然后将地点的值传给展示页面,让展示页面接收到地点后再连接对应的API并调用数据。

1.首页

代码如下(示例):

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportActionBar().hide();
        ImmersionBar.with(this)
                //.statusBarColor(R.color.purple_200)     //不写默认透明
                .init();

        TextView textView=findViewById(R.id.text1);
        EditText editText=findViewById(R.id.address);

        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String address=editText.getText().toString();
                Intent intent=new Intent(MainActivity.this,ShowActivity.class);
                intent.putExtra("address",address);
                startActivity(intent);

            }
        });
        editText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(editText.getText().toString()!=null){
                    editText.setText("");
                }
            }
        });

    }

2.展示页面

代码如下(示例):

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show);
        getSupportActionBar().hide();
        ImmersionBar.with(this)
                //.statusBarColor(R.color.purple_200)     //不写默认透明
                .init();

        TextView textView=findViewById(R.id.textView);
        TextView tem=findViewById(R.id.textView3);
        TextView wind=findViewById(R.id.textView5);
        TextView aqi=findViewById(R.id.textView6);
        TextView weather=findViewById(R.id.textView4);
        TextView ziwaixian=findViewById(R.id.textView16);

        TextView t2=findViewById(R.id.weathertoday);
        TextView t3=findViewById(R.id.temtoday);
        TextView t4=findViewById(R.id.qualtoday);

        TextView m2=findViewById(R.id.weathertomo);
        TextView m3=findViewById(R.id.temtomo);
        TextView m4=findViewById(R.id.qualtomo);

        Intent intent=getIntent();
        String address=getIntent().getStringExtra("address");
        textView.setText(address);

        //c5494be085dd41dca6ec4ffa59c9ed6a
        String code="APPCODE "+"c5494be085dd41dca6ec4ffa59c9ed6a";
        Api api=RetrofitUtils.getRetrofit("https://ali-weather.showapi.com/").create(Api.class);
        Call<WeatherResult> weatherResultCall=api.getWeather(code,address);
        weatherResultCall.enqueue(new Callback<WeatherResult>() {
            @Override
            public void onResponse(Call<WeatherResult> call, Response<WeatherResult> response) {
                WeatherResult weatherResult = response.body();

                t2.setText(weatherResult.showapi_res_body.f1.day_weather);
                t3.setText(weatherResult.showapi_res_body.f1.day_air_temperature+"/"+weatherResult.showapi_res_body.f1.night_air_temperature+"℃");
                t4.setText(weatherResult.showapi_res_body.f1.jiangshui);

                m2.setText(weatherResult.showapi_res_body.f2.day_weather);
                m3.setText(weatherResult.showapi_res_body.f2.day_air_temperature+"/"+weatherResult.showapi_res_body.f2.night_air_temperature+"℃");
                m4.setText(weatherResult.showapi_res_body.f2.jiangshui);

                tem.setText(weatherResult.showapi_res_body.now.temperature+"℃");
                weather.setText(weatherResult.showapi_res_body.now.weather);
                wind.setText(weatherResult.showapi_res_body.now.wind_direction+weatherResult.showapi_res_body.now.wind_power+" "+weatherResult.showapi_res_body.now.sd);
                aqi.setText("aqi:"+weatherResult.showapi_res_body.now.aqi);
                ziwaixian.setText("紫外线强度:"+weatherResult.showapi_res_body.f1.ziwaixian);

            }
            @Override
            public void onFailure(Call<WeatherResult> call, Throwable t) {

            }
        });
    }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • 如何通过Android Stduio来编写一个完整的天气预报APP

    目录 一.项目概述 二.功能分析 三.开发环境 四.涉及知识点 五.项目演示 六.项目总结 七.项目源码 一.项目概述 本次项目主要实现了天气预报功能.通过调用天气预报接口来获得天气数据,用LIstView和GridView来搭建每个界面,将查询的天气信息存储在SQLiteDatabase中.应用包括了五大界面: 首页面:有各种组件装饰,显示天气.温度.风向及各种指数,还有未来三天的天气城市管理界面:在这里可以对添加的城市进行管理搜索更多城市页面:可以添加想要查看的城市删除城市页面:可以删除已经

  • Android天气预报app改进版

    最近总是有人来和我说我以前写的一个小app无法正常获取数据~Android简易版天气预报app 今天就又运行了下来查找问题,发现或许是接口有限制吧,不能在多台手机使用同个apikey 然后,发现了我写的代码实在乱七八糟,界面也实在不好看,就又重写了一遍,小小地修改了一遍,开发环境改为了Android Studio 最终效果图如下 工程图如下 一.获取地区信息 做这么一个天气预报app,首先就要获取到国内地区列表 (在我的另一篇博客有介绍:向任意网址发起数据请求) 中国天气网开放有天气预报接口,访

  • Android天气预报之基于HttpGet对象解析天气数据的方法

    本文实例所述为Android天气预报之解析天气数据的代码,可实现获取HttpGet对象读取天气网站天气数据,并从数据中解析出天气数据,比如温度.温度.风力.风向.未来几天天气趋势.当天天气状况.空气污染指数等信息,还包括了调用对应的图片或天气动画文件,对于开发android天气预报程序的可以参考本文实例. 具体功能代码如下: import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayL

  • android JSON解析数据 android解析天气预报

    概要 笔者近期做到对天气预报JSON数据解析,在此小记. 天气预报接口:http://wthrcdn.etouch.cn/weather_mini?citykey=101200101 JSON数据如下: { "desc": "OK", "status": 1000, "data": { "wendu": "14", "ganmao": "天气转凉,空气湿度较

  • Android编程实现类似天气预报图文字幕垂直滚动效果的方法

    本文实例讲述了Android编程实现类似天气预报图文字幕垂直滚动效果的方法.分享给大家供大家参考,具体如下: 在很多天气或者新闻的应用中,我们都能看到一些字幕滚动的效果,最简单的实现为跑马灯效果,用系统提供的属性即可实现. 复杂一些的就需要自己去用自定义控件实现. 比如 让TextView 实现垂直滚动. 这里我要讲的是垂直滚动的字幕效果,并且内容并不仅为文字,还可以加入图片或者其他元素. 废话不多说,还是直接上效果图: 首先还是看一下核心的实现: 目前我的做法是重写了ScrollView,对外

  • Android编程实现获取新浪天气预报数据的方法

    本文实例讲述了Android编程实现获取新浪天气预报数据的方法.分享给大家供大家参考,具体如下: 新浪天气预报地址: http://php.weather.sina.com.cn/xml.php?city=武汉&password=DJOYnieT8234jlsK&day=0 其中,city后的城市可用java.net.URLEncoder.encode("武汉"," gb2312");也可以直接写"武汉",但不能用"wu

  • android调用国家气象局天气预报接口json数据格式解释

    国家气象局提供了三种数据的形式 网址在: http://www.weather.com.cn/data/sk/101010100.html http://www.weather.com.cn/data/cityinfo/101010100.html http://m.weather.com.cn/data/101010100.html 最后一种是解析最全面的. 数据解析格式: 第一个网址提供的json数据为: 复制代码 代码如下: {"weatherinfo":{"city&

  • Android Internet应用实现获取天气预报的示例代码

    在Eclipse中创建Android项目,利用之前学过的WebView控件和中国天气网提供的天气数据接口,实现获取指定城市的天气预报. 布局文件: res/layout/main.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:too

  • Android简单实现天气预报App

    本文实例为大家分享了Android简单实现天气预报App的具体代码,供大家参考,具体内容如下 一.UI设计 首页UI <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmln

  • Android studio编写简单的手电筒APP

    很不错的手电筒APP,分享给大家,希望大家喜欢. 1. Java代码     package com.example.goo.gfire; import android.app.Activity; import android.hardware.Camera; import android.hardware.Camera.Parameters; import android.os.Bundle; import android.view.View; import android.widget.Bu

  • Android简单实现启动画面的方法

    本文实例讲述了Android简单实现启动画面的方法.分享给大家供大家参考,具体如下: 核心代码: package com.demo.app; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Handler; public class SplashActivity extends Activity { private final int S

  • Android简单判断某个APK是否已经安装的方法

    本文实例讲述了Android简单判断某个APK是否已经安装的方法.分享给大家供大家参考,具体如下: privateboolean isAppInstalled(String uri){ PackageManager pm = getPackageManager(); boolean installed =false; try{ pm.getPackageInfo(uri,PackageManager.GET_ACTIVITIES); installed =true; }catch(Package

  • Android简单实现屏幕下方Tab菜单的方法

    本文实例讲述了Android简单实现屏幕下方Tab菜单的方法.分享给大家供大家参考,具体如下: 看到很多热门的Android程序(如:新浪微博.腾讯微博.京东商城.淘宝.当当等等)使用选项卡风格作为程序界面的主框架结构,而Android的选项卡控件默认是按钮在上方的.我在网上看到有多种实现方法,这里提供一种个人觉得比较简单的.由于我对Android开发所知甚少,方法的优劣目前不好评价,欢迎各位提供更好的思路. 主要原理:设置 TabWidget 控件的 android:layout_alignP

  • 安卓(Android)开发之统计App启动时间

    前言 作为 Android 开发者,想必多多少少要接触启动速度优化相关的事情,当用户越来越多,产品的功能也随着迭代越来越多,App 逐渐变得臃肿是一件很常见的现象,甚至可以说是不可避免的现象,随之而来的工作就是优化 App 性能,其中最主要的一项就是启动速度优化.但本文的主角并不是启动速度优化,而是启动时间统计. 一.启动类型 工欲善其事,必先利其器.想要优化 App 的启动速度,必须有准确衡量启动时间的方法,否则优化完之后效果怎样,自己都不知道,说出去别人也不信服不是.在做 App 启动时间统

  • Android如何动态改变App桌面图标

    时不时的我们就会发现,一些我们常见的应用,比如某宝,某东,在一些特殊的日子中,比如双十一,元旦,为了迎合这样一个日子的气氛,在桌面的应用图标就会发生改变,其实对于这样的一个桌面图标更换,Android中为我们提供了AndroidManifest.xml里的<activity-alias>标签实现方式(更多文章请关注我的微信公众账号,左边二维码). 我们知道,我们每写一个 Activity就要在AndroidManifest进行配置一下,我们才可以正常的启动它,除此之外,我们还可以对它设置一个别

  • iOS毕业设计之天气预报App

    9月中旬,开始动手做我的毕业设计了,之前一直在纠结做啥,后来想想,既然是做毕业设计,那就大胆地做点自己没接触过的东西吧.然后网上查找资料得知做天气预报需要用到开放的API,而且要用那种现在还在维护的,而且又免费的(对于我们学生党来说,这个是挺好的).天气预报App的核心是从天气API请求数据,请求得到的一般是JSON数据(这个JSON数据之前都没接触过),然后把JSON数据解析,然后在视图上显示出来.总得来说,这里应该涉及到"网络请求","JSON解析"这两个大块吧

随机推荐