Android Studio 单击按钮时从另一个片段加载片段

7nbnzgx9  于 2022-11-16  发布在  Android
关注(0)|答案(1)|浏览(141)

我是一个全新的移动的应用程序开发和排序跌跌撞撞我的方式通过这个应用程序,但我一直在努力尝试和解决这个问题。我想要无数的教程,寻找开发者网站和搜索Stack Overflow,但我做的似乎没有工作,请你能帮助我吗?我正在使用Android Studio //Dolphin | 2021.3.1 Patch 1和编码在Kotlin
我所要做的就是在fragment_delivery中单击按钮android:id="@+id/button_customer_sign"时打开fragment_sign,然后在客户签名后,在fragment_sign中单击按钮android:id="@+id/button_load_sign"时返回fragment_delivery
我的build.gradle中有以下内容

dependencies {

    implementation 'androidx.core:core-ktx:1.9.0'
    implementation 'androidx.appcompat:appcompat:1.5.1'
    implementation 'com.google.android.material:material:1.7.0'
    implementation 'androidx.annotation:annotation:1.5.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.5.1'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.5.3'
    implementation 'androidx.navigation:navigation-ui-ktx:2.5.3'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'com.kyanogen.signatureview:signature-view:1.2'

    implementation 'androidx.fragment:fragment-ktx:1.6.0-alpha03'
    implementation 'androidx.fragment:fragment:1.6.0-alpha03'
    debugImplementation 'androidx.fragment:fragment-testing:1.6.0-alpha03'

    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'

    // google support library ---------------------------------------------------------------------
    implementation 'androidx.appcompat:appcompat:1.5.1'
    implementation 'com.google.android.material:material:1.7.0'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'androidx.recyclerview:recyclerview:1.2.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'androidx.vectordrawable:vectordrawable:1.1.0'
    implementation 'androidx.browser:browser:1.4.0'
}

目前这是我的DeliveryFragment.kt中的内容

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import com.example.delivery.R
import com.example.delivery.databinding.FragmentDeliveryBinding
import com.example.delivery.ui.sign.Sign

import android.support.v4.app.Fragment //This import is an error
import android.support.v4.app.FragmentManager //This import is an error
import android.support.v4.app.FragmentTransaction //This import is an error

class DeliveryFragment : Fragment() {

    private var _binding: FragmentDeliveryBinding? = null
    private var customerSign: Button? = null

    // This property is only valid between onCreateView and
    // onDestroyView.
    private val binding get() = _binding!!

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val deliveryViewModel =
            ViewModelProvider(this).get(DeliveryViewModel::class.java)

        _binding = FragmentDeliveryBinding.inflate(inflater, container, false)
        val root: View = binding.root

        val customer = resources.getStringArray(R.array.customer_list)
        val arrayAdapter = ArrayAdapter(requireContext(), R.layout.dropdown_item, customer)
        binding.autoCompleteTextView.setAdapter(arrayAdapter)
        return root
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContectView(com.example.delivery.R.layout.content_main)
        customerSign = findViewById(com.example.delivery.R.id.button_customer_sign) as Button
        customerSign.setOnClickListener(View.OnClickListener { openSignFragment() })
    } //This entire method is an error

    fun openSignFragment() {
        val intent = Intent(this, SignFragment::class.java)
        startActivity(intent)
    } //This entire method is an error

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}

listeneropen arguments只是一个又一个错误,所以是这个.kt文件的最后3个导入。我需要找到这个,因为我还有很多其他的按钮要尝试和解决。
请能有人协助我,预先谢谢。

t30tvxxf

t30tvxxf1#

Intent需要Activity示例和类名,而fragment需要主机容器Activity正在运行。如果你转到另一个Activity,你将不得不再次使用它来在Activity容器中显示你的fragment。
请从onCreate中删除setContentView(),因为您在片段的onCreateView中返回了视图。
setContentView()使用布局填充当前窗口。
片段视图显示在Activity窗口内的容器中。
在片段onCreateView中只返回一次视图,在您的示例中,它是return root
如果使用导航组件,请使用此选项-〉

/** Please Execute this Code in onViewCreated method of your fragment **/

val navController = Navigation.findNavController(view)
navController.navigate(R.id.action_deliveryFragment_to_signFragment) 
//make sure you have defined the action in your navigation graph.

或者,如果您不使用导航组件,则使用此选项-〉

val fragManager = requireActivity().supportFragmentManager
          val transaction = fragManager.beginTransaction()
          transaction.replace(
              R.id.container,
              RecordFragment()
          )
          transaction.addToBackStack(null) // if u want this fragment to stay in stack specify it
          transaction.commit()

相关问题