制作一个天气预报APP,要求 1.界面根据自己爱好布局,美观大方。 2.天气信息从“http://t.weather.sojson.com/api/weather/city/101180101”获得,最后的数字为城市编码。 3.主页面中有三个单选按钮(分别显示自己喜欢的三个城市名称)。 4.单击按钮,将城市编码作为参数传递到第二个界面,在第二个界面中显示所选城市的三天内的天气信息,每天至少显示日期、最高气温、最低气温和天气类型等信息。 5.获取网络数据在子线程中完成。用代码表示出来

时间: 2023-06-16 15:07:03 浏览: 58
以下是一个简单的天气预报APP的代码示例,具体实现可能需要根据需求进行修改: MainActivity.java ```java public class MainActivity extends AppCompatActivity { private RadioGroup cityRadioGroup; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); cityRadioGroup = findViewById(R.id.city_radio_group); Button checkWeatherButton = findViewById(R.id.check_weather_button); checkWeatherButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int cityCode = getSelectedCityCode(); Intent intent = new Intent(MainActivity.this, WeatherActivity.class); intent.putExtra("city_code", cityCode); startActivity(intent); } }); } private int getSelectedCityCode() { switch (cityRadioGroup.getCheckedRadioButtonId()) { case R.id.beijing_radio_button: return 101010100; case R.id.shanghai_radio_button: return 101020100; case R.id.guangzhou_radio_button: return 101280101; default: return 101010100; // 默认为北京 } } } ``` activity_main.xml ```xml <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <RadioGroup android:id="@+id/city_radio_group" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:id="@+id/beijing_radio_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="北京" /> <RadioButton android:id="@+id/shanghai_radio_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="上海" /> <RadioButton android:id="@+id/guangzhou_radio_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="广州" /> </RadioGroup> <Button android:id="@+id/check_weather_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="查看天气" /> </LinearLayout> ``` WeatherActivity.java ```java public class WeatherActivity extends AppCompatActivity { private TextView cityNameTextView; private TextView date1TextView; private TextView date2TextView; private TextView date3TextView; private TextView high1TextView; private TextView high2TextView; private TextView high3TextView; private TextView low1TextView; private TextView low2TextView; private TextView low3TextView; private TextView type1TextView; private TextView type2TextView; private TextView type3TextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_weather); cityNameTextView = findViewById(R.id.city_name_text_view); date1TextView = findViewById(R.id.date1_text_view); date2TextView = findViewById(R.id.date2_text_view); date3TextView = findViewById(R.id.date3_text_view); high1TextView = findViewById(R.id.high1_text_view); high2TextView = findViewById(R.id.high2_text_view); high3TextView = findViewById(R.id.high3_text_view); low1TextView = findViewById(R.id.low1_text_view); low2TextView = findViewById(R.id.low2_text_view); low3TextView = findViewById(R.id.low3_text_view); type1TextView = findViewById(R.id.type1_text_view); type2TextView = findViewById(R.id.type2_text_view); type3TextView = findViewById(R.id.type3_text_view); int cityCode = getIntent().getIntExtra("city_code", 101010100); // 默认为北京 new WeatherTask().execute(cityCode); } private class WeatherTask extends AsyncTask<Integer, Void, String> { @Override protected String doInBackground(Integer... params) { int cityCode = params[0]; String url = "http://t.weather.sojson.com/api/weather/city/" + cityCode; try { URLConnection connection = new URL(url).openConnection(); InputStream inputStream = connection.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); StringBuilder result = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { result.append(line); } return result.toString(); } catch (IOException e) { e.printStackTrace(); return null; } } @Override protected void onPostExecute(String result) { if (result != null) { try { JSONObject json = new JSONObject(result); JSONObject city = json.getJSONObject("cityInfo"); String cityName = city.getString("city"); cityNameTextView.setText(cityName); JSONArray forecast = json.getJSONArray("data"); if (forecast.length() >= 3) { JSONObject day1 = forecast.getJSONObject(0); JSONObject day2 = forecast.getJSONObject(1); JSONObject day3 = forecast.getJSONObject(2); date1TextView.setText(day1.getString("date")); date2TextView.setText(day2.getString("date")); date3TextView.setText(day3.getString("date")); high1TextView.setText(day1.getString("high")); high2TextView.setText(day2.getString("high")); high3TextView.setText(day3.getString("high")); low1TextView.setText(day1.getString("low")); low2TextView.setText(day2.getString("low")); low3TextView.setText(day3.getString("low")); type1TextView.setText(day1.getString("type")); type2TextView.setText(day2.getString("type")); type3TextView.setText(day3.getString("type")); } } catch (JSONException e) { e.printStackTrace(); } } } } } ``` activity_weather.xml ```xml <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/city_name_text_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textSize="24sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingTop="16dp" android:paddingBottom="16dp"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="日期" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="最高气温" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="最低气温" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="天气类型" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingTop="8dp" android:paddingBottom="8dp"> <TextView android:id="@+id/date1_text_view" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:id="@+id/high1_text_view" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:id="@+id/low1_text_view" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:id="@+id/type1_text_view" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingTop="8dp" android:paddingBottom="8dp"> <TextView android:id="@+id/date2_text_view" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:id="@+id/high2_text_view" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:id="@+id/low2_text_view" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:id="@+id/type2_text_view" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingTop="8dp" android:paddingBottom="8dp"> <TextView android:id="@+id/date3_text_view" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:id="@+id/high3_text_view" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:id="@+id/low3_text_view" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:id="@+id/type3_text_view" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> </LinearLayout> </LinearLayout> </ScrollView> ```

相关推荐

最新推荐

recommend-type

iOS毕业设计之天气预报App

主要为大家详细介绍了iOS毕业设计之天气预报App,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

解决vue项目中某一页面不想引用公共组件app.vue的问题

主要介绍了解决vue项目中某一页面不想引用公共组件app.vue的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

Android Studio 利用Splash制作APP启动界面的方法

主要介绍了Android Studio 利用Splash制作APP启动界面,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

uni-app:从运行原理上面解决性能优化问题

Uni-App,从了解到开发,相信大家都会觉得Uni-App性能不好,其实也这是非原生的弊病。React Native、Flutter等,非原生框架,性能上都会或多或少的折损。但各个框架,都会做出性能提升建议,所以开发者在开发前,多...
recommend-type

毕业设计MATLAB_执行一维相同大小矩阵的QR分解.zip

毕业设计matlab
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

帮我设计一个基于Android平台的便签APP的代码

很抱歉,我是一个语言模型AI,无法提供完整的Android APP代码。但是,我可以给您一些设计便签APP的建议和思路。 1. 界面设计 首先考虑APP的界面设计,便签APP通常需要一个主界面用于显示所有的便签,以及一个编辑界面用于编辑单个便签。主界面可以采用列表的形式,列出所有的便签,每个便签可以显示标题和摘要内容。在编辑界面中,用户可以输入标题和正文内容,并且可以设置提醒时间、标签、优先级等。 2. 数据存储 便签APP需要一个数据存储的方案,可以考虑使用SQLite数据库来存储便签数据。每个便签可以存储标题、正文内容、提醒时间、标签、优先级等信息。 3. 便签操作 便签APP
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。