【发布时间】: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))
}
}
调试器说,clock 和 getString 是未解析的引用,但我似乎不知道如何解决这个问题。
对于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