本文实例为大家分享了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) {

            }
        });
    }

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

Android简单实现天气预报App的更多相关文章

  1. 详解如何通过H5(浏览器/WebView/其他)唤起本地app

    这篇文章主要介绍了详解如何通过H5(浏览器/WebView/其他)唤起本地app的相关资料,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  2. H5混合开发app如何升级的方法

    本篇文章主要介绍了H5混合开发app如何升级的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  3. html5 canvas合成海报所遇问题及解决方案总结

    这篇文章主要介绍了html5 canvas合成海报所遇问题及解决方案总结,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  4. Html5 video标签视频的最佳实践

    这篇文章主要介绍了Html5 video标签视频的最佳实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

  5. html5唤起app的方法

    这篇文章主要介绍了html5唤起app的方法的相关资料,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  6. HTML5在微信内置浏览器下右上角菜单的调整字体导致页面显示错乱的问题

    HTML5在微信内置浏览器下,在右上角菜单的调整字体导致页面显示错乱的问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧

  7. ios – containerURLForSecurityApplicationGroupIdentifier:在iPhone和Watch模拟器上给出不同的结果

    我使用默认的XCode模板创建了一个WatchKit应用程序.我向iOSTarget,WatchkitAppTarget和WatchkitAppExtensionTarget添加了应用程序组权利.(这是应用程序组名称:group.com.lombax.fiveminutes)然后,我尝试使用iOSApp和WatchKitExtension访问共享文件夹URL:延期:iOS应用:但是,测试NSURL

  8. xcode – 上传到App Store时进行身份验证

    只需为现有安装/文件夹创建备份,这很重要,因为在(新)安装期间,Transporter将删除以前的安装:现在运行以下命令来更新Transporter:希望这有助于某人.

  9. Ionic – Splash Screen适用于iOS,但不适用于Android

    我有一个离子应用程序,其中使用CLI命令离子资源生成的启动画面和图标iOS版本与正在渲染的启动画面完美配合,但在Android版本中,只有在加载应用程序时才会显示白屏.我检查了config.xml文件,所有路径看起来都是正确的,生成的图像出现在相应的文件夹中.(我使用了splash.psd模板来生成它们.我错过了什么?这是config.xml文件供参考,我觉得我在这里做错了–解决方法在config.xml中添加以下键:它对我有用!

  10. App store拒绝应用程序在iOs 10上支持IPV6网络

    我收到苹果公司的app拒绝邮件,下面是我们在连接到IPv6网络的Wi-Fi上运行iOS10.0.2的iPad和iPhone上查看了应用中的一个或多个错误.具体来说,应用程序在启动时仍保留在启动屏根据他们的要求,我已经在我的Mac上创建了NAT64网络,并为iPhone5S设备10.0.2os版本共享了互联网,App工作正常,但苹果称其不与IPv6合作任何人都可以确认我需要检查其他什么吗?

随机推荐

  1. Flutter 网络请求框架封装详解

    这篇文章主要介绍了Flutter 网络请求框架封装详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  2. Android单选按钮RadioButton的使用详解

    今天小编就为大家分享一篇关于Android单选按钮RadioButton的使用详解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

  3. 解决android studio 打包发现generate signed apk 消失不见问题

    这篇文章主要介绍了解决android studio 打包发现generate signed apk 消失不见问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

  4. Android 实现自定义圆形listview功能的实例代码

    这篇文章主要介绍了Android 实现自定义圆形listview功能的实例代码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  5. 详解Android studio 动态fragment的用法

    这篇文章主要介绍了Android studio 动态fragment的用法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  6. Android用RecyclerView实现图标拖拽排序以及增删管理

    这篇文章主要介绍了Android用RecyclerView实现图标拖拽排序以及增删管理的方法,帮助大家更好的理解和学习使用Android,感兴趣的朋友可以了解下

  7. Android notifyDataSetChanged() 动态更新ListView案例详解

    这篇文章主要介绍了Android notifyDataSetChanged() 动态更新ListView案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下

  8. Android自定义View实现弹幕效果

    这篇文章主要为大家详细介绍了Android自定义View实现弹幕效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  9. Android自定义View实现跟随手指移动

    这篇文章主要为大家详细介绍了Android自定义View实现跟随手指移动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  10. Android实现多点触摸操作

    这篇文章主要介绍了Android实现多点触摸操作,实现图片的放大、缩小和旋转等处理,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

返回
顶部