如何从片段Android Studio Kotlin调用任何活动 [英] how to call any activity from fragment Android Studio Kotlin
本文介绍了如何从片段Android Studio Kotlin调用任何活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以通过片段按钮"调用另一个活动吗?我已经看到了stackoverflow中的所有问题,但没有找到解决方法.
can i call another Activity From Fragment Button ? I have seen all the problems in stackoverflow, but have not found a solution.
这是我来自MainActivity的片段代码
this my fragment code from MainActivity
package com.example.apptest
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import kotlinx.android.synthetic.main.fragment_home.*
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
/**
* A simple [Fragment] subclass.
* Use the [HomeFragment.newInstance] factory method to
* create an instance of this fragment.
*/
class HomeFragment : Fragment() {
// TODO: Rename and change types of parameters
private var param1: String? = null
private var param2: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false)
}
companion object {
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment HomeFragment.
*/
// TODO: Rename and change types and number of parameters
@JvmStatic
fun newInstance(param1: String, param2: String) =
HomeFragment().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
}
这是我的xml片段代码
and this is my fragment xml code
<?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=".HomeFragment">
<TextView
android:textColor="#34495e"
android:textSize="20sp"
android:layout_marginTop="100dp"
android:id="@+id/mytitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
android:text="BELAJAR BERSAMA NATSUZD"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:textColor="#34495e"
android:textSize="20sp"
android:id="@+id/japantitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="日本語"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/mytitle" />
<Button
style="@style/Widget.AppCompat.Button.Colored"
android:textColor="#34495e"
android:layout_marginTop="50dp"
android:id="@+id/buttonImage"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:background="@drawable/custome_button"
android:drawableStart="@drawable/book50"
android:drawableLeft="@drawable/book50"
android:gravity="center"
android:padding="15dp"
android:text="KOSAKATA"
android:textSize="15sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/japantitle"
tools:ignore="OnClick" />
<Button
style="@style/Widget.AppCompat.Button.Colored"
android:textColor="#34495e"
android:text="BELAJAR SOAL"
android:gravity="center"
android:textSize="15sp"
android:textStyle="bold"
android:padding="15dp"
android:drawablePadding="15dp"
android:drawableLeft="@drawable/exam"
android:layout_marginRight="50dp"
android:layout_marginLeft="50dp"
android:layout_marginTop="20dp"
android:id="@+id/buttonImage1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/custome_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/buttonImage"
android:drawableStart="@drawable/exam" />
<Button
style="@style/Widget.AppCompat.Button.Colored"
android:textColor="#34495e"
android:text="PERCAKAPAN"
android:gravity="center"
android:textSize="15sp"
android:padding="15dp"
android:textStyle="bold"
android:drawablePadding="15dp"
android:drawableLeft="@drawable/chat"
android:layout_marginRight="50dp"
android:layout_marginLeft="50dp"
android:layout_marginTop="20dp"
android:id="@+id/buttonImage2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/custome_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/buttonImage1"
android:drawableStart="@drawable/chat" />
<Button
style="@style/Widget.AppCompat.Button.Colored"
android:textColor="#34495e"
android:text="POLA KALIMAT"
android:gravity="center"
android:textSize="15sp"
android:padding="15dp"
android:textStyle="bold"
android:drawablePadding="15dp"
android:drawableLeft="@drawable/variable"
android:layout_marginRight="50dp"
android:layout_marginLeft="50dp"
android:layout_marginTop="20dp"
android:id="@+id/buttonImage3"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/custome_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/buttonImage2"
android:drawableStart="@drawable/variable" />
</androidx.constraintlayout.widget.ConstraintLayout>
这是我的第二项活动(目标活动)
and this is my second activity ( target activity )
package com.example.apptest
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class SoalActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_soal)
}
}
如何使用片段中的按钮连接到第二个活动
how do I connect to the second activity using the button in the fragment
推荐答案
可以.
为了从片段中启动活动,请覆盖片段中的 onViewCreated()
方法.在其中输入:
In order to launch an activity from your fragment, override the onViewCreated()
method in your fragment. Inside it, type:
buttonImage2.setOnClickListener {
val intent = Intent(context, SoalActivity::class.java)
startActivity(intent)
}
就这样:)
这篇关于如何从片段Android Studio Kotlin调用任何活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文