我有一个非常简单的片段,它是我的应用程序的启动屏幕。它基本上只有两个按钮,当您单击 PLAY 按钮时,会使用 SoundPool 播放声音,并且应用程序会导航到另一个 Fragment。代码如下:
class FirstFragment : Fragment(R.layout.fragment_first) {
// Set up binding for FirstFragment
private var _binding: FragmentFirstBinding? = null
private val binding get() = _binding!!
// Prepare button views
private lateinit var playButton: Button
private lateinit var leaderboardButton: Button
// SoundPool for playing PLAY sound
private lateinit var soundPool: SoundPool
private var soundIdStart: Int = 0
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
_binding = FragmentFirstBinding.inflate(inflater, container, false)
return binding.root
}
override fun …Run Code Online (Sandbox Code Playgroud) 我正在开发一个简单的天气应用程序,并尝试以“K:mm a”格式显示时间(例如上午 6:30)。我正在获取用户搜索的指定地点(例如纽约市)的 Unix、UTC 时间戳。时间戳看起来类似于 1624836905,时区偏移量类似于 -14400。我有一个函数将两者相加,将其转换为毫秒,并应以指定的格式返回时间。函数如下:
fun dateTime(time: Int, zone: Int, format: String = "EEE, MMMM d K:mm a"): String {
return try {
val sdf = SimpleDateFormat(format)
val netDate = Date((time.plus(zone)).toLong() * 1000)
sdf.timeZone = TimeZone.getTimeZone("UTC")
sdf.format(netDate)
} catch (e: Exception) {
e.toString()
}
}
Run Code Online (Sandbox Code Playgroud)
我称之为:
sunriseTextView.text = dateTime(result2.lookup<Int>("daily.sunrise")[0], timeZone, "K:mm a")
sunsetTextView.text = dateTime(result2.lookup<Int>("current.sunset")[0], timeZone, "K:mm a")
Run Code Online (Sandbox Code Playgroud)
预期输出是日出/日落时间,例如上午 6:01 和晚上 9:05。我还在指定位置渲染了同样从 API 获得的当前时间。如下:
dateView.text = dateTime(result2.lookup<Int>("current.dt")[0], timeZone)
Run Code Online (Sandbox Code Playgroud)
它以“EEE, MMMM d K:mm a”的格式输出该地点的当前日期和时间(例如,Mon June 28 8:23 AM)。
当前时间始终是正确的,但是日出和日落时间存在问题。例如,如果我输入 …