Android简单实现天气预报App
作者:啦啦啦??? 发布时间:2022-11-05 05:50:32
标签: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) {
}
});
}
来源:https://blog.csdn.net/m0_56071898/article/details/124628812


猜你喜欢
- 本文研究的主要是Java ArrayList扩容问题实例详解的相关内容,具体介绍如下。首先我们需要知道ArrayList里面的实质的其实是一
- 通常在使用service更新应用时最常出现的问题就是Notification进度的更新问题、service在什么时间关闭以及需要我们自己在S
- 1.jsch官方API查看地址(附件为需要的jar) http://www.jcraft.com/jsch/2.jsch简介 JSch(Ja
- android系统提供了Environment.getExternalStorageDirectory()接口获得存储器的路径,但是这个接口
- 从Java 5开始,Java语言对方法参数支持一种新写法,叫 可变长度参数列表,其语法就是类型后跟...,表示此处接受的参数为0到多个Obj
- FileOutPutStream:子类,写出数据的通道步骤:1.获取目标文件2.创建通道(如果原来没有目标文件,则会自动创建一个)3.写入数
- 第一种(懒汉,线程不安全):public class Singleton { private static Singleton
- @Autowired注解和静态方法及new关系一、@autowired 与new new出来的对象无法调用@Autowired注入
- 本文实例讲述了Android编程解析XML文件的方法。分享给大家供大家参考,具体如下:前言在学习Android的Framework层源码时,
- 一、普遍的实现方式目前市面上的很多资源热修复方案基本上都是参考了 Instant Run的实现。简要说来,Instant Run中的资源热修
- 客户端代码using System;using System.Collections.Generic;using System.Compon
- 最近重新研究Java基础知识,发现以前太多知识知识略略带过了,比较说Java的线程机制,在Java中有两类线程:User Thread(用户
- 本文以实例讲述了android中DatePicker和TimePicker的使用方法,具体步骤如下:下面是实现具体功能的代码,其中main.
- 本文实例为大家分享了java实现猜拳小游戏的具体代码,供大家参考,具体内容如下实现下图要求public class User {privat
- 本文实例讲述了java图片滑动验证(登录验证)原理与实现方法。分享给大家供大家参考,具体如下:这是我简单做出的效果图,处理300X150px
- C# 中同样支持多维数组(也可称为矩形数组),它可以是二维的,也可以是三维的,多维数组中的数据以类似表格(行、列)的形式存储,因此也被称为矩
- Java异常是Java提供的一种识别及响应错误的一致性机制。Java异常机制可以使程序中异常处理代码和正常业务代码分离,保证程序代码更加优雅
- 第1部分 ArrayList介绍ArrayList简介ArrayList 是一个数组队列,相当于 动态数组。与Java中的数组相比,它的容量
- 1、介绍官网地址:https://www.yuque.com/easyexcel特点:1、Java领域解析、生成Excel比较有名的框架有A
- maven打包方式使用maven打包插件maven-jar-plugin在pom.xml文件最后新增以下代码。maven-dependenc