【问题标题】:OpenWeatherMap API开放天气地图 API
【发布时间】:2018-11-16 00:50:57
【问题描述】:
如何使用 openweathermap API 从 android 菜单中切换 °C / °F

菜单.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/degrees"
        android:title="Celsius / Fahrenheit"
        app:showAsAction="never"/>
</menu>

MainActivity.java

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.menu.menu:
            // How do change between Celsius and Fahrenheit
                break;


        }
        return true;
    }
}

Dropbox 链接可根据需要进行更深入的查看

Weather Test app

【问题讨论】:

  • 您提供的代码只是与UI相关,您还需要展示业务逻辑以查看发生了什么。
  • @sme 我的问题是我只显示开尔文度数,我想从菜单中更改它,但我真的不知道......
  • @sme 我会上传代码,不是很多,也许这样会更容易...

标签: android android-menu openweathermap


【解决方案1】:

来自OpenWeather Documentation.

单位格式

说明:

提供标准、公制和英制单位。

参数:

单位公制,英制。不使用单位参数时,格式默认为标准。

温度有华氏、摄氏和开尔文单位。

For temperature in Fahrenheit use units=imperial
For temperature in Celsius use units=metric
Temperature in Kelvin is used by default, no need to use units parameter in API call

所有 API 参数列表,单位为 openweathermap.org/weather-data

API 调用示例:

standard api.openweathermap.org/data/2.5/find?q=London
metric api.openweathermap.org/data/2.5/find?q=London&units=metric
imperial api.openweathermap.org/data/2.5/find?q=London&units=imperial

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-22
    • 2021-12-23
    • 1970-01-01
    • 2018-09-11
    • 2017-04-12
    • 2020-09-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多