我試著做一個應用程式來練習Android中的Layout,但我得到了一個錯誤:
我試著做一個應用程式來練習Android中的Layout。
未解決的參考:box_three_text
。
請看螢屏截圖。 [1]: https://i.stack.imgur.com/W3uGt.jpg
為什么教員的代碼可以作業,而我的卻不行呢?
這是我的代碼:
MainActivity.kt
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
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_fifth_text)
對于(item in clickableViews){
item.setOnClickListener{makeColored(it)}。
}
}
fun makeColored(view: View) {
當(view.id) {
// Boxes使用Color class顏色作背景
R.id.box_one_text -> view.setBackgroundColor(Color.DKGRAY)
R.id.box_two_text -> view.setBackgroundColor(Color.GRAY)
// Boxes使用Android顏色資源做背景
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)
else -> view.setBackgroundColor(Color.LTGRAY)
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"? >
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"/span>
xmlns:app="http://schemas.android.com/apk/res-auto"。
xmlns:tools="http://schemas.android.com/tools"。
android:id="@ id/constraint_layout"
android:layout_width="match_parent"。
android:layout_height="match_parent"。
tools:context=".MainActivity"/span>>
<TextView。
android:id="@ id/box_one_text"。
style="@style/WhiteBox"/span>
android:layout_width="0dp"。
android:layout_height="wrap_content"。
android:layout_marginStart="@dimen/margin_wide"
android:layout_marginTop="@dimen/margin_wide"。
android:layout_marginEnd="@dimen/margin_wide"。
android:fontFamily="@font/roboto"
android:text="@string/box_one"。
app:layout_constraintLeft_toLeftOf="father"
app:layout_constraintRight_toRightOf="parent"。
app:layout_constraintTop_toTopOf="parent"/span> />
<TextView
android:id="@ id/box_two_text"。
style="@style/WhiteBox"/span>
android:layout_width="130dp"。
android:layout_height="130dp"/span>
android:layout_marginStart="16dp"。
android:layout_marginTop="16dp"。
android:text="@string/box_two"/span>
app:layout_constraintStart_toStartOf="father"
app:layout_constraintTop_toBottomOf="@ id/box_one_text"/span> />
<TextView
android:id="@ id/box_three_text"/span>
style="@style/WhiteBox"/span>
android:layout_width="0dp"。
android:layout_height="wrap_content"。
android:layout_marginStart="16dp"。
android:layout_marginTop="16dp"。
android:layout_marginEnd="16dp"。
android:text="@string/box_three"
app:layout_constraintBottom_toTopOf="@ id/box_four_text"
app:layout_constraintEnd_toEndOf="father"
app:layout_constraintHorizontal_bias="0.0"/span>
app:layout_constraintStart_toEndOf="@ id/box_two_text"
app:layout_constraintTop_toTopOf="@ id/box_two_text" />
<TextView
android:id="@ id/box_four_text"。
style="@style/WhiteBox"/span>
android:layout_width="0dp"。
android:layout_height="wrap_content"。
android:layout_marginStart="16dp"。
android:layout_marginEnd="16dp"。
android:text="@string/box_four"/span>
app:layout_constraintBottom_toTopOf="@ id/box_five_text"/span>
app:layout_constraintEnd_toEndOf="father"
app:layout_constraintStart_toEndOf="@ id/box_two_text"
app:layout_constraintTop_toBottomOf="@ id/box_three_text"/span> />
<TextView
android:id="@ id/box_five_text"。
style="@style/WhiteBox"。
android:layout_width="0dp"。
android:layout_height="wrap_content"。
android:layout_marginStart="16dp"。
android:layout_marginEnd="16dp"。
android:text="@string/box_five"。
app:layout_constraintBottom_toBottomOf="@ id/box_two_text"。
app:layout_constraintEnd_toEndOf="father"
app:layout_constraintStart_toEndOf="@ id/box_two_text"
app:layout_constraintTop_toBottomOf="@ id/box_four_text"/span> />
</androidx.constraintlayout.widget.ConstraintLayout>
我的系統。Ubuntu 20.04 Android Studio 2021.1.1 Canary 11
uj5u.com熱心網友回復:
你必須在使用變數之前宣告這些變數。你可以使用資料庫系結/視圖系結/findViewById來實作。
做到以下幾點 :
class MainActivity : AppCompatActivity(){
private lateinit var box_one_text : TextView
private lateinit var box_two_text : TextView
private lateinit var box_three_text : TextView
private lateinit var box_four_text : TextView
private lateinit var box_five_text : TextView
override fun onCreate(sedInstanceState: Bundle? ){
super.onCreate(s savedInstanceState)
setContentView(R.layout.activity_main)
box_one_text = findViewById(R.id.box_one_text)
box_two_text = findViewById(R.id.box_two_text)
box_three_text = findViewById(R.id.box_three_text)
box_four_text = 依靠視圖查找(R.id.box_four_text)。
box_five_text = findViewById(R.id.box_five_text)
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)}。
}
}
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[/span]。
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)
else -> view.setBackgroundColor(Color.LTGRAY)
}
}
}
uj5u.com熱心網友回復:
該專案使用了Kotlin語法匯入。
你需要在你的MainActivity.kt
import kotlinx.android.synthetic.main.activity_main.*
并檢查你的build.gradle檔案(模塊)是否有kotlin-android-extensions插件
plugins {
id 'com.android.application'/span>
id 'kotlin-android'
id 'kotlin-android-extensions' >。
}
然而,你所遵循的例子可能已經過時了,目前建議從Kotlin語法遷移到Jetpack視圖系結
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/313059.html
標籤:
