如何从片段 Android Studio Kotlin 调用任何 activity

how to call any activity from fragment Android Studio Kotlin

我可以通过 Fragment Button 调用另一个 Activity 吗? 我在Whosebug中看到了所有的问题,但还没有找到解决方案。

这是我来自 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代码

<?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>

这是我的第二个 activity(目标 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)
    }
}

如何使用片段中的按钮连接到第二个 activity

当然可以。

为了从您的片段中启动 activity,请覆盖片段中的 onViewCreated() 方法。在里面,输入:

buttonImage2.setOnClickListener {
    val intent = Intent(context, SoalActivity::class.java)
    startActivity(intent)
}

就是这样:)