单击 ImageButton 时如何从 Fragment 发送到 Activity?

How to send from Fragment when clicking on ImageButton to Activity?

有必要在Fragment中,当你点击ImageButton时,Activity opens.I已经阅读了两天所有可能的资源并尝试了不同的方法,但是none 他们的工作。我在科特林写作。提前致谢)

P.S。一个学生,留给交作业的时间不多了,所以没把material的研究复杂化(保住项目后,以后一切正常学习)。

片段(fragment_add.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=".ui.add.AddFragment">

<ImageButton
    android:id="@+id/storageBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="24dp"
    android:background="#00FFFFFF"
    android:src="@drawable/im_storage"
    android:onClick="startActivity"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ImageButton
    android:id="@+id/transportationBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="50dp"
    android:background="#00FFFFFF"
    android:src="@drawable/im_transportation"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/storageBtn" />
</androidx.constraintlayout.widget.ConstraintLayout>

片段(AddFragment.kt):

package com.project_demo.kurzov.ui.add

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.project_demo.kurzov.R
import com.project_demo.kurzov.StorageActivity

class AddFragment : Fragment() {

    companion object {
        fun newInstance() = AddFragment()

    }

    private lateinit var viewModel: AddViewModel

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        View view =  inflater.inflate(R.layout.fragment_add, container, false)
        imageButton = (ImageButton)view.findViewById(R.id.storageBtn)
        imageButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                startActivity(getActivity(),StorageActivity.class)
            }
        })
        return view}
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        viewModel = ViewModelProvider(this).get(AddViewModel::class.java)
        // TODO: Use the ViewModel
    }
}

这里我试着照着做。有必要在按下按钮时将其发送到 (StorageActivity).

托管和管理 Fragments (BottomNavigationActivity.kt) 的主要Activity:

package com.project_demo.kurzov

import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.Button
import android.widget.ImageButton
import com.google.android.material.bottomnavigation.BottomNavigationView
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.ktx.auth
import com.google.firebase.ktx.Firebase
import com.project_demo.kurzov.databinding.ActivityBottomNavigationBinding

class BottomNavigationActivity : AppCompatActivity() {
    lateinit var binding: ActivityBottomNavigationBinding
    lateinit var auth: FirebaseAuth

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityBottomNavigationBinding.inflate(layoutInflater)
        setContentView(binding.root)
        auth = Firebase.auth
        auth.currentUser

        val navView: BottomNavigationView = binding.navView

        val navController = findNavController(R.id.nav_host_fragment_activity_bottom_navigation)
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        val appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.navigation_home,
                R.id.navigation_folder,
                R.id.navigation_add_documents,
                R.id.navigation_calendar,
                R.id.navigation_menu
            )
        )
        setupActionBarWithNavController(navController, appBarConfiguration)
        navView.setupWithNavController(navController)

    }

}

Activity (activity_bottom_navigation.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"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/bot_navigation"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu" />

    <fragment
        android:id="@+id/nav_host_fragment_activity_bottom_navigation"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/nav_view"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>

我将非常感谢帮助。

您需要创建一个 Intent 对象的实例并将其传递给 startActivity 函数:

imageButton.setOnClickListener {
    startActivity(Intent(requireContext(), StorageActivity::class.java))
}

我们使用 requireContext 的原因是因为我们在片段中使用它,如果我们也打算使用 activity,我们会使用“this”。

AddFragment.kt(片段到 Activity)

storageBtn.setOnClickListener {          
        startActivity(requireContext(),StorageActivity::class.java)
 }

activity 至 activity

storageBtn.setOnClickListener{
        startActivity(this,StorageActivity::class.java)
    }