【问题标题】:Updating a clock in main activity在主要活动中更新时钟
【发布时间】:2019-12-24 11:40:48
【问题描述】:

我正在尝试进入 Android 开发并想创建一个简单的应用程序,该应用程序在 TextView 中显示当前时间并每分钟更新一次。
我设法初始化了时间,但不知道如何更新它。

我搜索了很多,现在正在尝试创建一个更新 TextView 的广播接收器。
这就是我目前得到的:

AndroidMainfest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.alarmcompanion">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".TimeBroadcastReceiver"  android:exported="false">
            <intent-filter>
                <action android:name="android.intent.action.TIME_SET"/>
                <action android:name="android.intent.action.TIMEZONE_CHANGED"/>
            </intent-filter>
        </receiver>
    </application>

</manifest>

MainActivity.kt

package com.example.alarmcompanion

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        clock.text = getString(R.string.time, Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE))

    }
}
class TimeBroadcastReceiver : BroadcastReceiver() {
    override fun onReceive( context: Context, intent: Intent){
        clock.text = getString(R.string.time, Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE))
    }
}

调试器说,clockgetString 是未解析的引用,但我似乎不知道如何解决这个问题。
对于clock,我收到此错误:

    Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
    public val Activity.clock: TextView! defined in kotlinx.android.synthetic.main.activity_main
    public val Dialog.clock: TextView! defined in kotlinx.android.synthetic.main.activity_main
    public val android.app.Fragment.clock: TextView! defined in kotlinx.android.synthetic.main.activity_main
    public val androidx.fragment.app.Fragment.clock: TextView! defined in kotlinx.android.synthetic.main.activity_main

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">

    <TextView
        android:id="@+id/clock"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:clickable="false"
        android:gravity="center"
        android:text="@string/time"
        android:textColor="@android:color/background_light"
        android:textSize="120sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

如果我需要提供更多信息,请告诉我。

任何帮助或解释将不胜感激。
我是否走在正确的轨道上?

【问题讨论】:

    标签: android kotlin broadcastreceiver


    【解决方案1】:
    R.string.time
    

    是您的字符串 xml 资源文件标记上的链接,它可能不包含此字符串。 getString(id: Int) 是 Context 中的一个函数。在接收器中,您应该像 context.getString(id) 那样调用它。你的接收者对 TextView 一无所知,它只是 Activity 的变量。您可以将其上的链接共享为全局变量。但这是一种不好的做法,可能会导致内存泄漏!另一种方法是使您的接收器成为活动的内部类

    【讨论】:

    • 您好,感谢您的回答。该字符串位于 xml 资源文件中,用于主活动中时钟的初始设置。我相信如果这会是问题所在,时钟甚至不会在启动时设置。
    • 尝试添加 R 类导入。我在您的代码中没有看到它。某样东西 - 导入 com.example.alarmcompanion.R
    • 很遗憾这也不起作用。我认为问题不在于R.string.time 部分。由于某种原因,它不知道 TextView clock 或函数 getString。我猜我必须以某种方式导入视图,但我不知道如何......
    • 重要的是没有 R 类你不能被它的 id 刺痛。我编辑了我的答案
    • 谢谢,修复了getString 部分。你有让clock 为人所知的想法吗?我在问题中添加了错误
    【解决方案2】:

    如果有人偶然发现这篇文章,我在这里找到了答案: https://stackoverflow.com/a/13059819/9552448

    代码似乎属于 MainActivity 类。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-07
      • 1970-01-01
      • 2019-10-29
      • 2020-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多