簡體   English   中英

如何在 Kotlin 中使用多個按鈕打開活動

[英]How to open activities with multiple buttons in Kotlin

我有一個帶有 3 個按鈕的活動,我希望每個按鈕都能引導其他活動。 我用 KOTLIN 編碼!

XML代碼

   <Button
       android:id="@+id/btn_1"
       android:layout_width="150dp"
       android:layout_height="48dp"
       android:layout_marginTop="32dp"
       android:background="@drawable/custom_buttons"
       android:text="Characters"
       android:textColor="@color/white"
       android:textStyle="normal"
       app:flow_verticalAlign="top"
       app:layout_constraintBottom_toBottomOf="@+id/imageView4"
       app:layout_constraintEnd_toEndOf="@+id/imageView4"
       app:layout_constraintHorizontal_bias="0.498"
       app:layout_constraintStart_toStartOf="parent"
       app:layout_constraintTop_toBottomOf="@+id/toolbar"
       app:layout_constraintVertical_bias="0.31" />

   <Button
       android:id="@+id/btn_2"
       android:layout_width="150dp"
       android:layout_height="48dp"
       android:background="@drawable/custom_buttons"
       android:text="Matchs Up"
       android:textColor="@color/white"
       app:layout_constraintBottom_toBottomOf="@+id/imageView"
       app:layout_constraintEnd_toEndOf="parent"
       app:layout_constraintStart_toStartOf="@+id/imageView"
       app:layout_constraintTop_toBottomOf="@+id/imageView4"
       app:flow_verticalAlign="center"/>

   <Button
       android:id="@+id/btn_3"
       android:layout_width="150dp"
       android:layout_height="48dp"
       android:background="@drawable/custom_buttons"
       android:text="glossary"
       android:textColor="@color/white"
       app:layout_constraintBottom_toBottomOf="@+id/imageView5"
       app:layout_constraintEnd_toEndOf="parent"
       app:layout_constraintStart_toStartOf="@+id/imageView5"
       app:layout_constraintTop_toBottomOf="@+id/imageView" />

MainActivity.KT

    val button: Button = findViewById<Button>(R.id.btn_1)
    val button: Button = findViewById<Button>(R.id.btn_2)
    val button: Button = findViewById<Button>(R.id.btn_3)
    
    button.setOnClickListener{
        val intent = Intent(this, Page1::class.java)
        startActivity(intent)
    }
    button.setOnClickListener{
        val intent = Intent(this, Page2::class.java)
        startActivity(intent)
    }
    button.setOnClickListener{
        val intent = Intent(this, Page3::class.java)
        startActivity(intent)
    }

我在“val button: Button”上有一個紅色錯誤 -> Conflicting declarations: val button: Button, val button: Button。

我做錯了什么,請問有更好的方法嗎?

在同一個 scope 中不能有 2 個或多個同名變量。

val button意思是: “創建一個名為 button 的新(不可變)變量”。 要解決此問題,請為每個變量分配一個唯一名稱。 例如:

val button1: Button = findViewById<Button>(R.id.btn_1)
val button2: Button = findViewById<Button>(R.id.btn_2)
val button3: Button = findViewById<Button>(R.id.btn_3)

button1.setOnClickListener{
    val intent = Intent(this, Page1::class.java)
    startActivity(intent)
}
button2.setOnClickListener{
    val intent = Intent(this, Page2::class.java)
    startActivity(intent)
}
button3.setOnClickListener{
    val intent = Intent(this, Page3::class.java)
    startActivity(intent)
}

我假設您是 Kotlin 的新手。 強烈建議查看官方 Kotlin 文檔並閱讀 Kotlin 中的基本語法或變量

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM