Android studio Kotlin 多个按钮打开多个activity
Android studio Kotlin multiple buttons to open multiple activity
我有 5 个按钮。我有一个可以工作,但我不能让其他 4 个工作。这是第一个按钮的代码:
//MainActivity.kt
fun next(view: View){
val intent = Intent(this, SecondActivity::class.java)
startActivity(intent)
}
然后我就在我的第一个按钮上点击了。
//fragment_dashboard.xml
android:onClick="next"
我希望其他 4 个按钮可以打开特定的 activity,例如按钮 1 将打开 activity“第 I 章”。我一直在尝试使用多个意图,但效果不佳。关于如何为其他按钮创建 onClick 侦听器的任何建议?
这是我放置 5 个按钮的 fragment_dashboard.xml:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="fragments.DashboardFragment">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="148dp"
android:layout_marginTop="85dp"
android:layout_marginEnd="149dp"
android:text="Chapter I"
android:textColor="@color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:onClick="next" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="146dp"
android:layout_marginTop="50dp"
android:layout_marginEnd="146dp"
android:text="Chapter II"
android:textColor="@color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="143dp"
android:layout_marginTop="48dp"
android:layout_marginEnd="144dp"
android:text="Chapter III"
android:textColor="@color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button2" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="143dp"
android:layout_marginTop="53dp"
android:layout_marginEnd="144dp"
android:text="Chapter IV"
android:textColor="@color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button3" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="146dp"
android:layout_marginTop="47dp"
android:layout_marginEnd="146dp"
android:layout_marginBottom="208dp"
android:text="Chapter V"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button4" />
</androidx.constraintlayout.widget.ConstraintLayout>
!更新!
我尝试了另一种方法来为我的按钮设置 onclick。它没有显示任何错误,但每次我 运行 我的应用程序都会在我 运行 它的那一刻崩溃。
val button = findViewById<Button>(R.id.button)
button.setOnClickListener{
val intent = Intent(this, ChapterIAct::class.java)
startActivity(intent)
}
val button2 = findViewById<Button>(R.id.button2)
button2.setOnClickListener {
val intent1 = Intent(this, ChapterIIAct::class.java)
startActivity(intent1)
}
val button3 = findViewById<Button>(R.id.button3)
button3.setOnClickListener {
val intent2 = Intent(this, ChapterIIIAct::class.java)
startActivity(intent2)
}
val button4 = findViewById<Button>(R.id.button4)
button4.setOnClickListener{
val intent3 = Intent(this, ChapterIVAct::class.java)
startActivity(intent3)
}
val button5 = findViewById<Button>(R.id.button5)
button5.setOnClickListener{
val intent4 = Intent(this, ChapterVAct::class.java)
startActivity(intent4)
}
Button onClicks
!更新!
我将 onclick 放在片段内的 onCreateView 中。
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_dashboard, container, false)
val button = view.findViewById<Button>(R.id.button)
button.setOnClickListener{
val intent = Intent(this, ChapterIAct::class.java)
startActivity(intent)
}
return view
}
但 Intent 有错误“None 以下函数可以使用提供的参数调用。
(上下文!,Class<*>!)在 android.content.Intent 中定义
(String!, Uri!) 在 android.content.Intent
中定义
Intent error
我刚刚注意到这些按钮都在片段中。你应该这样做。
onCreateView
函数内部
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_blank, container, false)
val button = view.findViewById<Button>(R.id.button) //add view
button.setOnClickListener{
val intent = Intent(context, ChapterIAct::class.java)
startActivity(intent)
}
//all here same like at top
return view
我有 5 个按钮。我有一个可以工作,但我不能让其他 4 个工作。这是第一个按钮的代码:
//MainActivity.kt
fun next(view: View){
val intent = Intent(this, SecondActivity::class.java)
startActivity(intent)
}
然后我就在我的第一个按钮上点击了。
//fragment_dashboard.xml
android:onClick="next"
我希望其他 4 个按钮可以打开特定的 activity,例如按钮 1 将打开 activity“第 I 章”。我一直在尝试使用多个意图,但效果不佳。关于如何为其他按钮创建 onClick 侦听器的任何建议?
这是我放置 5 个按钮的 fragment_dashboard.xml:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="fragments.DashboardFragment">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="148dp"
android:layout_marginTop="85dp"
android:layout_marginEnd="149dp"
android:text="Chapter I"
android:textColor="@color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:onClick="next" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="146dp"
android:layout_marginTop="50dp"
android:layout_marginEnd="146dp"
android:text="Chapter II"
android:textColor="@color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="143dp"
android:layout_marginTop="48dp"
android:layout_marginEnd="144dp"
android:text="Chapter III"
android:textColor="@color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button2" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="143dp"
android:layout_marginTop="53dp"
android:layout_marginEnd="144dp"
android:text="Chapter IV"
android:textColor="@color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button3" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="146dp"
android:layout_marginTop="47dp"
android:layout_marginEnd="146dp"
android:layout_marginBottom="208dp"
android:text="Chapter V"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button4" />
</androidx.constraintlayout.widget.ConstraintLayout>
!更新! 我尝试了另一种方法来为我的按钮设置 onclick。它没有显示任何错误,但每次我 运行 我的应用程序都会在我 运行 它的那一刻崩溃。
val button = findViewById<Button>(R.id.button)
button.setOnClickListener{
val intent = Intent(this, ChapterIAct::class.java)
startActivity(intent)
}
val button2 = findViewById<Button>(R.id.button2)
button2.setOnClickListener {
val intent1 = Intent(this, ChapterIIAct::class.java)
startActivity(intent1)
}
val button3 = findViewById<Button>(R.id.button3)
button3.setOnClickListener {
val intent2 = Intent(this, ChapterIIIAct::class.java)
startActivity(intent2)
}
val button4 = findViewById<Button>(R.id.button4)
button4.setOnClickListener{
val intent3 = Intent(this, ChapterIVAct::class.java)
startActivity(intent3)
}
val button5 = findViewById<Button>(R.id.button5)
button5.setOnClickListener{
val intent4 = Intent(this, ChapterVAct::class.java)
startActivity(intent4)
}
Button onClicks
!更新! 我将 onclick 放在片段内的 onCreateView 中。
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_dashboard, container, false)
val button = view.findViewById<Button>(R.id.button)
button.setOnClickListener{
val intent = Intent(this, ChapterIAct::class.java)
startActivity(intent)
}
return view
}
但 Intent 有错误“None 以下函数可以使用提供的参数调用。 (上下文!,Class<*>!)在 android.content.Intent 中定义 (String!, Uri!) 在 android.content.Intent
中定义Intent error
我刚刚注意到这些按钮都在片段中。你应该这样做。
onCreateView
函数内部
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_blank, container, false)
val button = view.findViewById<Button>(R.id.button) //add view
button.setOnClickListener{
val intent = Intent(context, ChapterIAct::class.java)
startActivity(intent)
}
//all here same like at top
return view