【安卓开发】天气预报app的UI界面(包含全部代码)

简介: 【安卓开发】天气预报app的UI界面(包含全部代码)

全部代码https://gitee.com/Cutele/weather-forecast-v1

增加了发送短信等功能

b站视频链接

主页面写的闪退了,就不记录了。

主要是借助适配器显示自己想要显示的页面。

城市管理

效果图

20200401134307494.png

activity_city_manager.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".city_manager.CityManagerActivity"
    android:background="@mipmap/bg"
    >
    <RelativeLayout
        android:id="@+id/city_top_layout"
        android:background="#F5F5F5"
        android:layout_width="match_parent"
        android:layout_height="50dp">
        <ImageView
            android:id="@+id/city_iv_back"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:src="@mipmap/icon_back"
            android:layout_marginLeft="10dp"
            ></ImageView>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/city_iv_delete"
            android:src="@mipmap/icon_write"
            android:layout_alignParentRight="true"
            android:layout_marginRight="10dp"
            ></ImageView>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/city_tv_title"
            android:text="城市管理"
            android:textSize="25dp"
            android:textStyle="bold"
            android:layout_centerInParent="true"
            ></TextView>
    </RelativeLayout>
    <ImageView
        android:id="@+id/city_line"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#DCDCDC"
        android:layout_below="@id/city_top_layout"
        ></ImageView>
    <ListView
        android:id="@+id/city_lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/city_line"
        android:divider="@null"
        ></ListView>
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/city_iv_add"
        android:src="@mipmap/icon_add"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_margin="30dp"
        android:padding="10dp"
        ></ImageView>
</RelativeLayout>

item_city_manager.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp">
    <androidx.cardview.widget.CardView
        android:id="@+id/item_city_cv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="3dp"
        app:cardCornerRadius="10dp"
        app:cardElevation="5dp"
        app:contentPadding="10dp">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/item_city_tv_city"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:text="北京"
                android:textSize="22sp"
                android:textStyle="bold" />
            <TextView
                android:id="@+id/item_city_tv_temp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="40sp"
                android:layout_alignParentRight="true"
                android:layout_marginRight="20dp"
                android:text="12℃"/>
            <ImageView
                android:id="@+id/item_city_line1"
                android:layout_width="match_parent"
                android:layout_height="2dp"
                android:layout_below="@id/item_city_tv_temp"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="10dp"
                android:background="#808A87"/>
            <TextView
                android:id="@+id/item_city_tv_condition"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="16sp"
                android:layout_below="@id/item_city_line1"
                android:text="天气:晴"/>
            <TextView
                android:id="@+id/item_city_line2"
                android:layout_width="2dp"
                android:layout_height="20dp"
                android:layout_toRightOf="@id/item_city_tv_condition"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:background="#808A87"
                android:layout_below="@id/item_city_line1"/>
            <TextView
                android:id="@+id/item_city_wind"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="16sp"
                android:layout_toRightOf="@id/item_city_line2"
                android:layout_below="@id/item_city_line1"
                android:text="西南风2级"/>
            <TextView
                android:id="@+id/item_city_temprange"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/item_city_line1"
                android:textSize="16sp"
                android:layout_alignParentRight="true"
                android:text="16~9℃"
                android:layout_marginRight="20dp"/>
        </RelativeLayout>
    </androidx.cardview.widget.CardView>
</LinearLayout>

CityManagerActivity.java

package com.example.weather.city_manager;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import com.example.weather.R;
import com.example.weather.db.DBManager;
import com.example.weather.db.DatabaseBean;
import java.util.ArrayList;
import java.util.List;
public class CityManagerActivity extends AppCompatActivity implements View.OnClickListener {
    ImageView addIv,backIv,deleteIv;
    ListView cityLv;
    List<String> mDatas;//列表数据源
    String[]hotCitys = {"北京","上海","广州","深圳","珠海","佛山","南京","苏州","厦门","长沙","成都","福州",
            "杭州","武汉","青岛","西安","太原","沈阳","重庆","天津","南宁"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_city_manager);
        cityLv = findViewById(R.id.city_lv);
        mDatas = new ArrayList<>();
        mDatas.add("北京");
        mDatas.add("上海");
        mDatas.add("广州");
        mDatas.add("深圳");
        cityLv.setAdapter(new CityManagerAdapter(this,mDatas));
    }
    @Override
    public void onClick(View view) {
    }
}

CityManagerAdapter.java

自定义适配器

package com.example.weather.city_manager;
import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.weather.R;
import com.example.weather.db.DatabaseBean;
import com.example.weather.juhe.JHTempBean;
import com.google.gson.Gson;
import java.util.List;
public class CityManagerAdapter extends BaseAdapter {
    Context context;
    List<String> mDatas;
    public CityManagerAdapter(Context context, List<String> mDatas) {
        this.context = context;
        this.mDatas = mDatas;
    }
    @Override
    public int getCount() {
        return mDatas.size();
    }
    @Override
    public Object getItem(int i) {
        return mDatas.get(i);
    }
    @Override
    public long getItemId(int i) {
        return i;
    }
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        View convertView = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_city_manager,null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        String bean = mDatas.get(i);
        holder.cityTv.setText(bean);
        return convertView;
    }
    static class ViewHolder{
        TextView cityTv,conTv,currentTempTv,windTv,tempRangeTv;
        public ViewHolder(View itemView){
            cityTv = itemView.findViewById(R.id.item_city_tv_city);
            conTv = itemView.findViewById(R.id.item_city_tv_condition);
            currentTempTv = itemView.findViewById(R.id.item_city_tv_temp);
            windTv = itemView.findViewById(R.id.item_city_wind);
            tempRangeTv = itemView.findViewById(R.id.item_city_temprange);
        }
    }
}

城市删除

效果图

20200401134307494.png

activity_delete_city.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:orientation="vertical"
    tools:context=".city_manager.DeleteCityActivity"
    android:background="@mipmap/bg"
    >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="#F5F5F5"
        android:id="@+id/delete_top_layout">
        <ImageView
            android:id="@+id/delete_iv_error"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:src="@mipmap/icon_error"
            android:layout_marginLeft="5dp"/>
        <ImageView
            android:id="@+id/delete_iv_right"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:src="@mipmap/icon_right"
            android:layout_alignParentRight="true"
            android:layout_marginRight="5dp"/>
        <TextView
            android:id="@+id/delete_tv_title"
            android:text="城市管理"
            android:layout_centerInParent="true"
            android:textSize="18sp"
            android:textStyle="bold"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RelativeLayout>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#DCDCDC"/>
    <ListView
        android:layout_below="@id/delete_top_layout"
        android:id="@+id/delete_lv"
        android:divider="@null"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>
</RelativeLayout>

item_deletecity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp">
    <androidx.cardview.widget.CardView
        android:id="@+id/item_delete_cv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        app:cardElevation="5dp"
        app:cardCornerRadius="10dp"
        app:contentPadding="20dp">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="60dp">
            <ImageView
                android:id="@+id/item_delete_iv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/icon_laji"
                android:layout_centerVertical="true"
                android:layout_marginLeft="10dp"
                />
            <TextView
                android:id="@+id/item_delete_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="北京"
                android:textSize="20sp"
                android:layout_toRightOf="@id/item_delete_iv"
                android:layout_centerVertical="true"
                android:layout_marginLeft="20dp"
                android:textStyle="bold"/>
        </RelativeLayout>
    </androidx.cardview.widget.CardView>
</RelativeLayout>

DeleteCityActivity.java

package com.example.weather.city_manager;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import com.example.weather.R;
import java.util.ArrayList;
import java.util.List;
public class DeleteCityActivity extends AppCompatActivity {
    ListView deleteLv;
    String[] hotCitys = {"北京","上海","广州","深圳","珠海","佛山","南京","苏州","厦门","长沙","成都","福州",
            "杭州","武汉","青岛","西安","太原","沈阳","重庆","天津","南宁"};
    List<String> mDatas;//列表数据源
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_delete_city);
        deleteLv = findViewById(R.id.delete_lv);//找到组件
        //传入数据
        mDatas = new ArrayList<>();
        mDatas.add("北京");
        mDatas.add("上海");
        mDatas.add("广州");
        mDatas.add("深圳");
        //设置适配器
        deleteLv.setAdapter(new DeleteCityAdapter(this,mDatas));
    }
}

DeleteCityAdapter.java

package com.example.weather.city_manager;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.weather.R;
import java.util.List;
public class DeleteCityAdapter extends BaseAdapter {
    Context context;
    List<String> mDatas;
    List<String>deleteCitys;
    private LayoutInflater inflater;
    public DeleteCityAdapter(Context context, List<String> mDatas) {
        this.context = context;
        this.mDatas = mDatas;
    }
    @Override
    public int getCount() {
        return mDatas.size();
    }
    @Override
    public Object getItem(int i) {
        return mDatas.get(i);
    }
    @Override
    public long getItemId(int i) {
        return i;
    }
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        DeleteCityAdapter.ViewHolder holder = null;
        View convertView = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_deletecity,null);
            holder = new DeleteCityAdapter.ViewHolder(convertView);
            convertView.setTag(holder);
        }else{
            holder = (DeleteCityAdapter.ViewHolder) convertView.getTag();
        }
        String bean = mDatas.get(i);
        holder.cityTv.setText(bean);
        return convertView;
    }
    class ViewHolder{
        TextView cityTv,conTv,currentTempTv,windTv,tempRangeTv;
        public ViewHolder(View itemView){
            cityTv = itemView.findViewById(R.id.item_delete_tv);
        }
    }
}

城市搜索

效果图

20200401134307494.png

activity_search_city.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:padding="10dp"
    android:background="@mipmap/bg">
  <ImageView
      android:id="@+id/search_iv_submit"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentRight="true"
      android:layout_marginLeft="5dp"
      android:src="@mipmap/icon_search"/>
  <EditText
      android:id="@+id/search_et"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:padding="3dp"
      android:layout_toLeftOf="@id/search_iv_submit"
      android:hint="搜索国内城市:"
      android:background="@drawable/searchbg"/>
  <TextView
      android:id="@+id/search_tv"
      android:layout_below="@id/search_et"
      android:layout_marginTop="20dp"
      android:textSize="16sp"
      android:textStyle="bold"
      android:text="热门城市"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
  <GridView
      android:id="@+id/search_gv"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_below="@id/search_tv"
      android:numColumns="5"></GridView>
</RelativeLayout>

item_hotcity.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:gravity="center"
    android:text="北京"
    android:background="@drawable/frag_bg">
</TextView>

SearchCityActivity.java

package com.example.weather.city_manager;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import com.example.weather.R;
public class SearchCityActivity extends AppCompatActivity implements View.OnClickListener{
    EditText searchEt;
    ImageView submitIv;
    GridView searchGv;
    String[]hotCitys = {"北京","上海","广州","深圳","珠海","佛山","南京","苏州","厦门","长沙","成都","福州",
            "杭州","武汉","青岛","西安","太原","沈阳","重庆","天津","南宁"};
    private ArrayAdapter<String> adapter;
    String city;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //setContentView(R.layout.activity_search_city);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_city);
        searchEt = findViewById(R.id.search_et);
        submitIv = findViewById(R.id.search_iv_submit);
        searchGv = findViewById(R.id.search_gv);
        submitIv.setOnClickListener(this);
//        设置适配器
        adapter = new ArrayAdapter<>(this, R.layout.item_hotcity, hotCitys);
        searchGv.setAdapter(adapter);
     //   setListener();
    }
    @Override
    public void onClick(View view) {
    }
}


目录
相关文章
|
6天前
|
搜索推荐 Android开发 开发者
探索安卓开发中的自定义视图:打造个性化UI组件
【10月更文挑战第39天】在安卓开发的世界中,自定义视图是实现独特界面设计的关键。本文将引导你理解自定义视图的概念、创建流程,以及如何通过它们增强应用的用户体验。我们将从基础出发,逐步深入,最终让你能够自信地设计和实现专属的UI组件。
|
12天前
|
JavaScript C++ 容器
【Azure Bot Service】部署NodeJS ChatBot代码到App Service中无法自动启动
2024-11-12T12:22:40.366223350Z Error: Cannot find module 'dotenv' 2024-11-12T12:40:12.538120729Z Error: Cannot find module 'restify' 2024-11-12T12:48:13.348529900Z Error: Cannot find module 'lodash'
36 11
|
8天前
|
Android开发 Swift iOS开发
探索安卓与iOS开发的差异和挑战
【10月更文挑战第37天】在移动应用开发的广阔舞台上,安卓和iOS这两大操作系统扮演着主角。它们各自拥有独特的特性、优势以及面临的开发挑战。本文将深入探讨这两个平台在开发过程中的主要差异,从编程语言到用户界面设计,再到市场分布的不同影响,旨在为开发者提供一个全面的视角,帮助他们更好地理解并应对在不同平台上进行应用开发时可能遇到的难题和机遇。
|
10天前
|
XML 存储 Java
探索安卓开发之旅:从新手到专家
【10月更文挑战第35天】在数字化时代,安卓应用的开发成为了一个热门话题。本文旨在通过浅显易懂的语言,带领初学者了解安卓开发的基础知识,同时为有一定经验的开发者提供进阶技巧。我们将一起探讨如何从零开始构建第一个安卓应用,并逐步深入到性能优化和高级功能的实现。无论你是编程新手还是希望提升技能的开发者,这篇文章都将为你提供有价值的指导和灵感。
|
8天前
|
存储 API 开发工具
探索安卓开发:从基础到进阶
【10月更文挑战第37天】在这篇文章中,我们将一起探索安卓开发的奥秘。无论你是初学者还是有经验的开发者,这篇文章都将为你提供有价值的信息和建议。我们将从安卓开发的基础开始,逐步深入到更复杂的主题,如自定义组件、性能优化等。最后,我们将通过一个代码示例来展示如何实现一个简单的安卓应用。让我们一起开始吧!
|
9天前
|
存储 XML JSON
探索安卓开发:从新手到专家的旅程
【10月更文挑战第36天】在这篇文章中,我们将一起踏上一段激动人心的旅程,从零基础开始,逐步深入安卓开发的奥秘。无论你是编程新手,还是希望扩展技能的老手,这里都有适合你的知识宝藏等待发掘。通过实际的代码示例和深入浅出的解释,我们将解锁安卓开发的关键技能,让你能够构建自己的应用程序,甚至贡献于开源社区。准备好了吗?让我们开始吧!
21 2
|
10天前
|
Android开发
布谷语音软件开发:android端语音软件搭建开发教程
语音软件搭建android端语音软件开发教程!
|
13天前
|
移动开发 Java Android开发
探索Android与iOS开发的差异性与互联性
【10月更文挑战第32天】在移动开发的大潮中,Android和iOS两大平台各领风骚。本文将深入浅出地探讨这两个平台的开发差异,并通过实际代码示例,展示如何在各自平台上实现相似的功能。我们将从开发环境、编程语言、用户界面设计、性能优化等多个角度进行对比分析,旨在为开发者提供跨平台开发的实用指南。
35 0
|
存储 缓存 安全
Android14 适配之——现有 App 安装到 Android14 手机上需要注意些什么?
Android14 适配之——现有 App 安装到 Android14 手机上需要注意些什么?
515 0
|
6月前
|
传感器 物联网 Android开发
【Android App】物联网中查看手机支持的传感器及实现摇一摇功能-加速度传感器(附源码和演示 超详细)
【Android App】物联网中查看手机支持的传感器及实现摇一摇功能-加速度传感器(附源码和演示 超详细)
199 1