更新 main activity 中的时钟

Updating a clock in main activity

我正在尝试进入 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>

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

如有任何帮助或解释,我们将不胜感激。
我走在正确的轨道上吗?

R.string.time

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

的 class 内

如果有人偶然发现这个 post,我在这里找到了答案:

该代码似乎属于 MainActivity class。