马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
高质量源码(非开源)
关注公众号《编程乐学》
背景复兴:24011501
目录
媒介
一、使命先容
1.1 背景
1.2目的和意义
二、 实现先容
视频演示
2.1 启动页实现
2.2注册页面实现
2.3 登岸页面实现
2.4 首页实现
2.5 都会管理列表页面实现
三、获取源码
媒介
在使用Android Studio开发天气预报应用步伐时,首先必要考虑提供给用户的功能和用户体验。这包罗设计直观友好的用户界面,与实时天气数据的交互以及用户权限管理。本文将领导读者使用Java语言和Android Studio创建一个天气预报应用步伐的初步框架,并先容如何获取实时天气数据以及在应用步伐中显现这些数据的方法。通过本文,读者将学习如何创建一个功能美满的天气预报应用步伐,并为用户提供实时正确的天气信息。
一、使命先容
1.1 背景
随着移动应用步伐的普及,天气预报应用步伐成为人们一样平常生活中不可或缺的一部分。无论是旅行、户外运动还是一样平常通勤,正确的天气信息对人们的生活有偏重要影响。因此,开发一款用户友好、功能美满的天气预报应用步伐具有重要意义。
1.2目的和意义
本文旨在指导读者使用Android Studio和Java语言开发一款天气预报应用步伐,从而使用户能够方便获取实时的天气信息。该应用步伐将资助用户做出更为正确的出行决策,进步一样平常生活中对天气变化的应对能力。另外,通过学习本文所先容的应用步伐开发过程,读者能够把握与API的交互、数据处理和用户界面设计等关键技能,为未来的移动应用开发奠定坚实的基础。
二、 实现先容
视频演示
Android studio期末设计大作业~天气预报App
2.1 启动页实现
- <?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="#ffffff"
- tools:context=".ui.StartActivity">
- <ImageView
- android:id="@+id/imageView"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:srcCompat="@drawable/logo" />
- </androidx.constraintlayout.widget.ConstraintLayout>
复制代码 2.2注册页面实现

在 onCreate 方法中,它初始化了一些控件,并设置了点击事件监听器。当用户点击注册按钮时,它会获取用户输入的用户名和暗码,并与重复暗码举行比较,然后根据注册结果显示相应的消息,并在注册成功时跳转到 LoginActivity 页面。
另外,它还提供了点击事件监听器,用于在用户点击"已有账号,去登录"时跳转到登录页。
- package com.example.weather.ui;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- import androidx.appcompat.app.AppCompatActivity;
- import com.example.weather.Data.DatabaseHelper;
- import com.example.weather.R;
- public class RegisterActivity extends AppCompatActivity {
- private EditText mUserNameEditText;
- private EditText mPasswordEditText,mRePasswordEditText;
- private Button registerButton;
- private TextView loginTv;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_register);
- // 获取控件
- mUserNameEditText = findViewById(R.id.username_edittext);
- mPasswordEditText = findViewById(R.id.password_edittext);
- mRePasswordEditText = findViewById(R.id.repassword);
- registerButton = findViewById(R.id.register_button);
- loginTv = findViewById(R.id.tv_login);
-
- // 点击跳转登录页
- loginTv.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
- startActivity(intent);
- }
- });
- // 注册成功后跳转登录页
- registerButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // 获取用户输入的内容
- String username = mUserNameEditText.getText().toString().trim();
- String password = mPasswordEditText.getText().toString().trim();
- String repassword = mRePasswordEditText.getText().toString().trim();
- if (username.isEmpty() || password.isEmpty()|| repassword.isEmpty()) {
- Toast.makeText(getApplicationContext(), "请输入账号或密码或重复密码", Toast.LENGTH_SHORT).show();
- return;
- }
- if (result) {
- Toast.makeText(getApplicationContext(), "注册成功", Toast.LENGTH_SHORT).show();
- Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
- startActivity(intent);
- finish();
- } else {
- Toast.makeText(getApplicationContext(), "注册失败", Toast.LENGTH_SHORT).show();
- }
- }
- });
- }
- }
复制代码 2.3 登岸页面实现

- <?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="#ffffff"
- tools:context=".ui.LoginActivity">
- <ImageView
- android:id="@+id/imageView3"
- android:layout_width="0dp"
- android:layout_height="230dp"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:srcCompat="@drawable/logo" />
- <LinearLayout
- android:id="@+id/linearLayout2"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_marginStart="24dp"
- android:layout_marginTop="8dp"
- android:layout_marginEnd="24dp"
- android:orientation="vertical"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toBottomOf="@+id/imageView3">
- <TextView
- android:id="@+id/textView"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="用户名"
- android:textColor="#000000"
- android:textSize="14sp"
- android:textStyle="bold" />
- <EditText
- android:id="@+id/user"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="10dp"
- android:ems="10"
- android:hint="请输入用户名"
- android:inputType="textPersonName"/>
- <TextView
- android:id="@+id/textView5"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="20dp"
- android:text="密码"
- android:textColor="#000000"
- android:textSize="14sp"
- android:textStyle="bold" />
- <EditText
- android:id="@+id/pass"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="10dp"
- android:ems="10"
- android:hint="请输入用户密码"
- android:inputType="textPassword" />
- </LinearLayout>
- <Button
- android:id="@+id/login_button"
- android:layout_width="0dp"
- android:layout_height="40dp"
- android:layout_marginStart="32dp"
- android:layout_marginTop="32dp"
- android:layout_marginEnd="32dp"
- android:background="@drawable/login"
- android:text="立 即 登 录 "
- android:textColor="#fff"
- android:textSize="16sp"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.0"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toBottomOf="@+id/linearLayout2" />
- <TextView
- android:id="@+id/login_register"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="8dp"
- android:text="新用户,立即注册!"
- android:textColor="#00b5ff"
- android:textStyle="bold"
- app:layout_constraintEnd_toEndOf="@+id/login_button"
- app:layout_constraintStart_toStartOf="@+id/login_button"
- app:layout_constraintTop_toBottomOf="@+id/login_button" />
- </androidx.constraintlayout.widget.ConstraintLayout>
复制代码 在 onCreate 方法中,它设置了一个延迟执行的 runnable 和一个计时器。计时器用于在一段时间后移除延迟执行的 runnable,以防止页面跳转后仍旧执行跳转逻辑。
在 tomainActive 方法中,它启动了一个跳转到 LoginActivity 的 Intent,并在跳转完成后调用了 finish 方法注销当前页面。
- package com.example.weather.ui;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- import androidx.appcompat.app.AppCompatActivity;
- import com.example.weather.Data.DatabaseHelper;
- import com.example.weather.R;
- public class LoginActivity extends AppCompatActivity {
- private TextView loginRegister;
- private EditText user;
- private EditText pass;
- private Button mLoginButton;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- initView();
- register();
- home();
- }
- // 登陆成功后跳转首页
- private void home() {
- mLoginButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // 获取用户输入的内容
- String username = user.getText().toString().trim();
- String password = pass.getText().toString().trim();
- if (username.isEmpty() || password.isEmpty()) {
- Toast.makeText(getApplicationContext(), "请输入账号或密码", Toast.LENGTH_SHORT).show();
- return;
- }
- boolean result = mDatabaseHelper.checkUser(username, password);
- if (result) {
- Toast.makeText(getApplicationContext(), "登陆成功", Toast.LENGTH_SHORT).show();
- Intent intent = new Intent(LoginActivity.this, MainActivity.class);
- startActivity(intent);
- } else {
- Toast.makeText(getApplicationContext(), "账号或密码错误", Toast.LENGTH_SHORT).show();
- }
- }
- });
- }
- // 点击跳转注册页
- private void register() {
- loginRegister.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
- startActivity(intent);
- }
- });
- }
- // 获取控件
- private void initView() {
- user = findViewById(R.id.user);
- pass = findViewById(R.id.pass);
- mLoginButton = findViewById(R.id.login_button);
- loginRegister = findViewById(R.id.login_register);
- }
- }
复制代码 2.4 首页实现
首页重要实现当前都会的天气情况,24小时和未来7天还有生活指数的显示
初始化控件:通过findViewById()方法获取布局文件中的各个控件。
check()方法:用于查询都会信息,根据通报过来的都会信息设置默认都会,并将都会信息显示在TextView中。
addCity()方法:用于添加都会,当点击添加按钮时,跳转到都会选择界面。
nowWeather()方法:用于获取当天实时天气信息。首先获取都会名称,然后通过Volley库发送网络哀求,获取JSON数据。剖析JSON数据,将实时天气信息设置给对应的TextView,并根据天气切换对应的图标。还处理了RecyclerView的数据源,并将数据显示在RecyclerView中。末了,设置生活指数信息,将指数信息显示在页面中。
futureWeather()方法:用于获取未来七每天气信息。发送网络哀求获取JSON数据,剖析JSON数据,将天气信息封装到FutureBean对象中,并设置到RecyclerView中显示。
initView()方法:用于初始化控件,通过findViewById()方法获取各个控件的引用。
2.5 都会管理列表页面实现

- <?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="#ffffff"
- tools:context=".ui.CityActivity">
- <View
- android:id="@+id/view2"
- android:layout_width="0dp"
- android:layout_height="40dp"
- android:background="#00b5ff"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- <ImageView
- android:id="@+id/img_back"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginStart="16dp"
- app:layout_constraintBottom_toBottomOf="@+id/view2"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:srcCompat="@drawable/back" />
- <TextView
- android:id="@+id/textView25"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="城市管理"
- android:textColor="#ffffff"
- android:textSize="20sp"
- app:layout_constraintBottom_toBottomOf="@+id/view2"
- app:layout_constraintEnd_toEndOf="@+id/view2"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- <ImageView
- android:id="@+id/img_add"
- android:layout_width="20dp"
- android:layout_height="20dp"
- android:layout_marginEnd="16dp"
- app:layout_constraintBottom_toBottomOf="@+id/view2"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintTop_toTopOf="@+id/view2"
- app:srcCompat="@drawable/add_city" />
- <ListView
- android:id="@+id/lv"
- android:layout_width="0dp"
- android:layout_height="0dp"
- android:layout_marginTop="8dp"
- android:background="#ffffff"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toBottomOf="@+id/view2" />
- </androidx.constraintlayout.widget.ConstraintLayout>
复制代码 在refreshListView方法中,通过适配器清空数据,然后从数据库中获取全部数据,并将它们添加到适配器中显示。如果适配器中没有数据,则显示提示信息。末了调用适配器的notifyDataSetChanged方法刷新列表视图。
在add方法中,点击添加按钮时,会弹出一个对话框,用户可以在对话框中输入都会名称。点击确定按钮后,会判断输入的都会是否为空和是否已存在于数据库中,如果都满意,则将都会添加到数据库中,并清空输入框的内容。末了关闭对话框并刷新列表视图。
在delete方法中,长按列表项会弹出一个对话框,扣问用户是否确认删除选中的都会。如果确认删除,则将选中的都会从数据库中删除,并显示删除成功的提示信息。末了刷新列表视图。
在toDetails方法中,点击都会名称会跳转到天气页面。将选中的都会信息通过Intent通报给MainActivity,并启动MainActivity。同时,竣事当前的CityActivity。
在back方法中,点击返回按钮会竣事当前的CityActivity,返回上一个页面。
在initView方法中,获取布局中的各个控件,并举行必要的初始化操作。此中,通过CityAdapter创建一个适配器,并将其设置为ListView的适配器。然后调用refreshListView方法来显示数据库中的数据。
到此为止,我们的项目就已经完成了!
|