From 23b479a9b5ff121f40e5a50f0ad6df6599a86df5 Mon Sep 17 00:00:00 2001 From: JRNitre Date: Tue, 17 Dec 2024 19:31:40 +0800 Subject: [PATCH] =?UTF-8?q?2024-12-17=20=E5=AE=8C=E5=96=84=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F=E5=8A=9F=E8=83=BD=EF=BC=8CSDK=20=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E8=B0=83=E8=AF=95=E5=AE=8C=E6=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/example/weather/MainActivity.java | 117 ++++++++++++++- .../example/weather/WeatherCardActivity.java | 1 + .../com/example/weather/view/moreInfo.java | 62 ++++++++ .../main/res/drawable/rounded_background.xml | 15 ++ app/src/main/res/layout/activity_about.xml | 133 ++++++++++++++++++ app/src/main/res/layout/activity_main.xml | 6 +- app/src/main/res/layout/mpopupwindow.xml | 10 ++ app/src/main/res/layout/popup_info.xml | 72 ++++++++++ app/src/main/res/mipmap-hdpi/icon_back.png | Bin 0 -> 1718 bytes app/src/main/res/mipmap-hdpi/more_bg.png | Bin 0 -> 1146 bytes app/src/main/res/mipmap-hdpi/more_cache.png | Bin 0 -> 1593 bytes app/src/main/res/mipmap-hdpi/more_share.png | Bin 0 -> 1561 bytes app/src/main/res/mipmap-hdpi/more_version.png | Bin 0 -> 1751 bytes 13 files changed, 410 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/com/example/weather/view/moreInfo.java create mode 100644 app/src/main/res/drawable/rounded_background.xml create mode 100644 app/src/main/res/layout/activity_about.xml create mode 100644 app/src/main/res/layout/popup_info.xml create mode 100644 app/src/main/res/mipmap-hdpi/icon_back.png create mode 100644 app/src/main/res/mipmap-hdpi/more_bg.png create mode 100644 app/src/main/res/mipmap-hdpi/more_cache.png create mode 100644 app/src/main/res/mipmap-hdpi/more_share.png create mode 100644 app/src/main/res/mipmap-hdpi/more_version.png diff --git a/app/src/main/java/com/example/weather/MainActivity.java b/app/src/main/java/com/example/weather/MainActivity.java index 20b7667..350e69e 100644 --- a/app/src/main/java/com/example/weather/MainActivity.java +++ b/app/src/main/java/com/example/weather/MainActivity.java @@ -13,9 +13,11 @@ import android.graphics.drawable.Drawable; import android.os.Build; import android.os.Bundle; import android.util.Log; +import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.Button; import android.widget.LinearLayout; import android.widget.PopupWindow; import android.widget.TextView; @@ -40,6 +42,7 @@ import com.example.weather.data.MyCity; import com.example.weather.data.RainData; import com.example.weather.util.QuoteUtil; import com.example.weather.util.SpfUtil; +import com.example.weather.view.moreInfo; import com.facebook.stetho.Stetho; import com.google.gson.Gson; import com.qweather.sdk.bean.base.Code; @@ -76,6 +79,10 @@ public class MainActivity extends AppCompatActivity implements AMapLocationListe private boolean state = false;//分钟级降水数据收缩 false 收缩 true 展开 private int wallpaper; private String imagePath; + private List saveCityList; // 获取保存的城市信息 + + + private PopupWindow infoPopupWindow; // 新增用于显示更多信息的 PopupWindow public static final String KEY_WALLPAPER = "wallpaper_id"; public static final String KEY_IMAGEPATH = "image_path"; @@ -96,7 +103,6 @@ public class MainActivity extends AppCompatActivity implements AMapLocationListe Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat(); String formattedTime = sdf.format(date).substring(11); - Log.v("test", formattedTime); // 地图接口 MapsInitializer.updatePrivacyShow(this, true, true); @@ -110,9 +116,14 @@ public class MainActivity extends AppCompatActivity implements AMapLocationListe hoursWeather = findViewById(R.id.hoursWeather); daysWeather = findViewById(R.id.daysWeather); + // 上一次更新天气的时间 + // TODO 这里的时间显示格式有问题 tv_lastTime.setText(tv_lastTime.getText() + formattedTime); + // 初始化数据库 cityDbOpenHelper = new CityDbOpenHelper(this); + + // 设置状态栏为透明 if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) { getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE ); @@ -134,6 +145,7 @@ public class MainActivity extends AppCompatActivity implements AMapLocationListe findViewById(R.id.tv_precMore).setOnClickListener(this); findViewById(R.id.tv_moreDays).setOnClickListener(this); findViewById(R.id.iv_add).setOnClickListener(this); + //和风sdk key HeConfig.init("CFB4NCKPV7", "c9762ba50c6249878a12aff7f1d8356f"); @@ -142,6 +154,16 @@ public class MainActivity extends AppCompatActivity implements AMapLocationListe // HeConfig.switchToBizService(); // 免费订阅 HeConfig.switchToDevService(); + + // 初始化完毕调用 SDK 获取天气信息 + saveCityList = new ArrayList<>(); + + saveCityList = cityDbOpenHelper.queryAllFromDb(); + // 调取保存的城市列表中的第一个城市进行显示 + String city = saveCityList.get(0).getCity_name(); + + tv_City.setText(city); + getCityId(city); } @Override @@ -161,13 +183,11 @@ public class MainActivity extends AppCompatActivity implements AMapLocationListe Intent intent = new Intent(this, MapActivity.class); startActivity(intent); } else if (view.getId() == R.id.tv_precMore) { - Log.d("hhz", "onClick: " + state); if (state) {//收缩 fore_rain.setVisibility(View.GONE); state = false; tvPrecMore.setText("查看详情"); } else {//展开 -// AnimationUtil.collapse(fore_rain, findViewById(R.id.tv_precMore)); fore_rain.setVisibility(View.VISIBLE); state = true; tvPrecMore.setText("收起详情"); @@ -196,6 +216,7 @@ public class MainActivity extends AppCompatActivity implements AMapLocationListe mPopupWindow.showAsDropDown(view, -100, 0); TextView changeCity = mPopupWindow.getContentView().findViewById(R.id.tv_change_city);//切换城市 TextView wallpaper = mPopupWindow.getContentView().findViewById(R.id.tv_wallpaper);//壁纸管理 + TextView more = mPopupWindow.getContentView().findViewById(R.id.tv_more);//更多 changeCity.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { @@ -216,6 +237,93 @@ public class MainActivity extends AppCompatActivity implements AMapLocationListe mPopupWindow.dismiss(); } }); + + more.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + if (mPopupWindow == null) { + View contentView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.mpopupwindow, null, false); + mPopupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true); + + // 设置属性 + mPopupWindow.setBackgroundDrawable(new ColorDrawable(0x0000)); // 设置pop透明效果 + mPopupWindow.setAnimationStyle(R.anim.cp_push_bottom_in); + mPopupWindow.setFocusable(true); // 设置pop获取焦点 + mPopupWindow.setTouchable(true); // 设置pop可点击 + mPopupWindow.setOutsideTouchable(true); // 设置点击pop外侧消失 + + // 设置点击监听器 + TextView changeCity = contentView.findViewById(R.id.tv_change_city); // 切换城市 + TextView wallpaper = contentView.findViewById(R.id.tv_wallpaper); // 壁纸管理 + TextView more = contentView.findViewById(R.id.tv_more); // 更多 + + changeCity.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + Intent intent = new Intent(MainActivity.this, WeatherCardActivity.class); + intent.putExtra("id", LocationID); + intent.putExtra("city", locateCity); + intent.putExtra("province", locateProvince); + startActivity(intent); + mPopupWindow.dismiss(); + } + }); + + wallpaper.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + Intent intent = new Intent(MainActivity.this, ChangeWallpaper.class); // 调用活动“ChangeWallpaper” + startActivity(intent); + mPopupWindow.dismiss(); + } + }); + + more.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + // 如果 infoPopupWindow 尚未初始化,则进行初始化 + if (infoPopupWindow == null) { + View popupView = LayoutInflater.from(MainActivity.this).inflate(R.layout.popup_info, null); + TextView infoText = popupView.findViewById(R.id.tv_info_text); + Button closeButton = popupView.findViewById(R.id.btn_close_popup); + + // 设置信息文本(可以根据需要动态设置) + // infoText.setText("这里是更多详细信息"); + + // 关闭按钮的点击监听器 + closeButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (infoPopupWindow != null && infoPopupWindow.isShowing()) { + infoPopupWindow.dismiss(); + } + } + }); + + // 初始化 infoPopupWindow + infoPopupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true); + infoPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); + infoPopupWindow.setOutsideTouchable(true); + infoPopupWindow.setFocusable(true); + } + + // 显示 infoPopupWindow + if (!infoPopupWindow.isShowing()) { + infoPopupWindow.showAtLocation(findViewById(android.R.id.content), Gravity.CENTER, 0, 0); + } + + // 避免同时显示两个 PopupWindow + if (mPopupWindow != null && mPopupWindow.isShowing()) { + mPopupWindow.dismiss(); + } + } + }); + } + + // 显示主 PopupWindow + mPopupWindow.showAsDropDown(findViewById(android.R.id.content).getRootView(), -100, 0); + } + }); } private void initLocation() { @@ -269,7 +377,7 @@ public class MainActivity extends AppCompatActivity implements AMapLocationListe MyCity myCity1 = new MyCity(locateCity, LocationID); cityDbOpenHelper.insertData(myCity1); Log.i(TAG, LocationID); - getNowTemp("101020100"); + getNowTemp(LocationID); getRain(lon, lat); getTodayTemp(LocationID); getHoursTemp(LocationID); @@ -489,6 +597,7 @@ public class MainActivity extends AppCompatActivity implements AMapLocationListe } }); } else { + // 如果没有可用警告信息,则调取一个名言显示 QuoteUtil.getQuote(new QuoteUtil.QuoteCallback() { @Override public void onSuccess(String quote) { diff --git a/app/src/main/java/com/example/weather/WeatherCardActivity.java b/app/src/main/java/com/example/weather/WeatherCardActivity.java index fd0a970..c089347 100644 --- a/app/src/main/java/com/example/weather/WeatherCardActivity.java +++ b/app/src/main/java/com/example/weather/WeatherCardActivity.java @@ -116,6 +116,7 @@ public class WeatherCardActivity extends AppCompatActivity implements View.OnCli @Override public void onLocate() { + // TODO 需要实现系统定位功能 //定位接口,需要APP自身实现,这里模拟一下定位 new Handler().postDelayed(new Runnable() { @Override diff --git a/app/src/main/java/com/example/weather/view/moreInfo.java b/app/src/main/java/com/example/weather/view/moreInfo.java new file mode 100644 index 0000000..1a5436d --- /dev/null +++ b/app/src/main/java/com/example/weather/view/moreInfo.java @@ -0,0 +1,62 @@ +package com.example.weather.view; + +import androidx.appcompat.app.AppCompatActivity; + +import android.app.AlertDialog; +import android.content.DialogInterface; +import android.content.Intent; +import android.content.SharedPreferences; +import android.content.pm.PackageInfo; +import android.content.pm.PackageManager; +import android.os.Bundle; +import android.view.View; +import android.widget.ImageView; +import android.widget.RadioGroup; +import android.widget.TextView; +import android.widget.Toast; + +import com.example.weather.R; + +public class moreInfo extends AppCompatActivity implements View.OnClickListener{ + TextView bgTv,cacheTv,versionTv,shareTv; + ImageView backIv; + private SharedPreferences pref; + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_about); + } + + private void setRGListener() { + } + + private String getVersionName() { + /* 获取应用的版本名称*/ + PackageManager manager = getPackageManager(); + String versionName = null; + try { + PackageInfo info = manager.getPackageInfo(getPackageName(), 0); + versionName = info.versionName; + } catch (PackageManager.NameNotFoundException e) { + e.printStackTrace(); + } + return versionName; + } + + @Override + public void onClick(View v) { + + } + + private void shareSoftwareMsg(String s) { + /* 分享软件文本的函数*/ + Intent intent = new Intent(Intent.ACTION_SEND); + intent.setType("text/plain"); + intent.putExtra(Intent.EXTRA_TEXT,s); + startActivity(Intent.createChooser(intent,"知我天气")); + } + + private void clearCache() { + + } +} \ No newline at end of file diff --git a/app/src/main/res/drawable/rounded_background.xml b/app/src/main/res/drawable/rounded_background.xml new file mode 100644 index 0000000..6b310aa --- /dev/null +++ b/app/src/main/res/drawable/rounded_background.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_about.xml b/app/src/main/res/layout/activity_about.xml new file mode 100644 index 0000000..fc0ea8b --- /dev/null +++ b/app/src/main/res/layout/activity_about.xml @@ -0,0 +1,133 @@ + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 43836ab..2c8cf6b 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -40,7 +40,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" - android:text="城市" + android:text="test" android:textColor="@color/white" android:textSize="25sp" /> @@ -267,7 +267,9 @@ + android:layout_height="wrap_content" + android:background="@drawable/rounded_background" + /> diff --git a/app/src/main/res/layout/mpopupwindow.xml b/app/src/main/res/layout/mpopupwindow.xml index dd9e9af..66a7a75 100644 --- a/app/src/main/res/layout/mpopupwindow.xml +++ b/app/src/main/res/layout/mpopupwindow.xml @@ -35,6 +35,16 @@ android:textColor="@color/black" android:textSize="16sp"/> + + \ No newline at end of file diff --git a/app/src/main/res/layout/popup_info.xml b/app/src/main/res/layout/popup_info.xml new file mode 100644 index 0000000..593d95f --- /dev/null +++ b/app/src/main/res/layout/popup_info.xml @@ -0,0 +1,72 @@ + + + + + + + + + + + + + + + + + +