android - 我的 Widget id 在 Android Studio 中显示错误
问题描述
所以我正在为一个颜色点击器做一个项目。我将 id 输入到 listOf Array 中,并且 ID 显示错误。
主要活动
import android.graphics.Color
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setListeners()
}
private fun setListeners() {
val clickableViews: List<View> =
listOf(box_one_text, box_two_text, box_three_text,
box_four_text, box_five_text)
for (item in clickableViews) {
item.setOnClickListener { makeColored(it) }
}
}
private fun makeColored(view: View) {
when (view.id) {
// Boxes using Color class colors for background
R.id.box_one_text -> view.setBackgroundColor(Color.DKGRAY)
R.id.box_two_text -> view.setBackgroundColor(Color.GRAY)
// Boxes using Android color resources for background
R.id.box_three_text -> view.setBackgroundResource(android.R.color.holo_green_light)
R.id.box_four_text -> view.setBackgroundResource(android.R.color.holo_green_dark)
R.id.box_five_text -> view.setBackgroundResource(android.R.color.holo_green_light)
// Boxes using custom colors for background
R.id.red_button -> box_three_text.setBackgroundResource(R.color.my_red)
R.id.yellow_button -> box_four_text.setBackgroundResource(R.color.my_yellow)
R.id.green_button -> box_five_text.setBackgroundResource(R.color.my_green)
else -> view.setBackgroundColor(Color.LTGRAY)
}
}
}
bulid.gradel(应用程序)
plugins {
id 'com.android.application'
id 'kotlin-android'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
defaultConfig {
applicationId "com.examples.colormyviews"
minSdkVersion 19
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" android:id="@+id/constraint_layout">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/box_one"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent" android:layout_marginBottom="16dp"
android:layout_marginTop="16dp" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintHorizontal_bias="0.0" app:layout_constraintVertical_bias="0.0"
android:id="@+id/box_one_text"
style="@style/WhiteBox"/>
<TextView
android:text="@string/box_two"
android:layout_width="130dp"
android:layout_height="130sp" android:id="@+id/box_two_text"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@+id/box_one_text" app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="16dp" style="@style/WhiteBox"/>
<TextView
android:text="@string/box_three"
android:layout_width="0dp"
android:layout_height="wrap_content" android:id="@+id/box_three_text"
app:layout_constraintTop_toTopOf="@+id/box_two_text"
android:layout_marginStart="16dp"
app:layout_constraintStart_toEndOf="@+id/box_two_text" android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="@+id/box_four_text"
style="@style/WhiteBox"/>
<TextView
android:text="@string/box_four"
android:layout_width="0dp"
android:layout_height="wrap_content" android:id="@+id/box_four_text"
app:layout_constraintTop_toBottomOf="@+id/box_three_text"
app:layout_constraintBottom_toTopOf="@+id/box_five_text" android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginStart="16dp"
app:layout_constraintStart_toEndOf="@+id/box_two_text" android:layout_marginTop="16dp"
android:layout_marginBottom="16dp" style="@style/WhiteBox"/>
<TextView
android:text="@string/box_five"
android:layout_width="0dp"
android:layout_height="wrap_content" android:id="@+id/box_five_text"
app:layout_constraintBottom_toBottomOf="@+id/box_two_text"
app:layout_constraintTop_toBottomOf="@+id/box_four_text" android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginStart="16dp"
app:layout_constraintStart_toEndOf="@+id/box_two_text" style="@style/WhiteBox"/>
<TextView
android:text="@string/how_to"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/textView"
android:textSize="24sp" android:textColor="@color/black"
app:layout_constraintBaseline_toBaselineOf="@+id/textView2" app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="16dp"/>
<TextView
android:text="@string/tap_directions"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/textView2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/box_two_text" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintVertical_bias="0.081"
app:layout_constraintStart_toEndOf="@+id/textView" app:layout_constraintHorizontal_bias="0.606"/>
<Button
android:text="@string/red_button_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/red_button"
app:layout_constraintBaseline_toBaselineOf="@+id/green_button"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintEnd_toStartOf="@+id/green_button"/>
<Button
android:text="@string/green_button_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/green_button"
app:layout_constraintStart_toEndOf="@+id/red_button"
app:layout_constraintHorizontal_bias="0.5" app:layout_constraintEnd_toStartOf="@+id/yellow_button"
app:layout_constraintTop_toBottomOf="@+id/textView2" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="1.0"/>
<Button
android:text="@string/yellow_button_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/yellow_button"
app:layout_constraintBaseline_toBaselineOf="@+id/green_button"
app:layout_constraintStart_toEndOf="@+id/green_button"
app:layout_constraintHorizontal_bias="0.5" app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
id 在 setListener 中很有趣,但它不起作用。我在其他项目中也遇到过这种情况,但我仍然没有找到解决这个问题的方法。如果有人有任何建议,那就太好了。
解决方案
所以我确实找到了怎么做。显然,自从 Android Studio 取出它后,您必须将它添加到您的项目中。
首先在您的 bulid.gradle (app) 文件中的 plugin 部分下和 Android 部分之前添加这个插件。
apply plugin: 'kotlin-android-extensions'
然后将此导入添加到您的 Main Activity 或您要使用它的其他地方
import kotlinx.android.synthetic.main.activity_main.*
这就是您所需要的,它应该按预期工作。
推荐阅读
- mysql - 有一个元素相等表,计算“唯一”元素的数量
- javascript - Carousel bootstrap 加载,但不滑动且控制无响应
- typo3 - 工作空间是 TYPO3 的一等公民吗?
- git - 向 .gitignore 添加几个项目
- f# - F#: 为什么这段代码会抛出 System.IO.FileNotFoundException: 'Could not load file or assembly 'System.Runtime, Version=4.1.2.0, ...'
- ios - 以编程方式创建应用程序时,如何在 Swift 4 中向 @objc 函数添加参数?
- python - 通过提供 url django 无法打开页面
- apache - 反向代理和网络服务器有什么区别?
- java - 从命令行运行时混合 java 和 kotlin 代码的 ClassNotFoundException
- html - Html5电影插入html作为背景?