【问题标题】:How to pass values from fragments in one activity to another activity? [duplicate]如何将值从一个活动中的片段传递到另一个活动? [复制]
【发布时间】:2021-08-02 02:23:47
【问题描述】:


我正在一个有两个活动的应用程序中做一个小测验。一项活动有问题的片段,每个问题都用editText 回答。这些片段包含在该活动内的FrameLayout 中,当我到达最后一个问题时,我有一个“完成”按钮。

我想要的是当有人完成测验并点击最后一个片段中的“完成”按钮时,它会将他移动到第二个活动并检查问题的结果。

换句话说,我如何从每个片段中获取在上一个活动中在editTexts 中输入的值,以便检查第二个活动中的答案并显示结果?

测验活动(主要活动)

package com.example.ishihara_test

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main_activity)

        val plate1 = Frag_Plate1()
        val plate2 = Frag_Plate2()
        val plate3 = Frag_Plate3()

        val btnPrev = findViewById<Button>(R.id.btnPrev)
        val btnNext = findViewById<Button>(R.id.btnNext)


        supportFragmentManager.beginTransaction().apply {
            replace(R.id.flfragment, plate1)
            commit()
        }

        btnPrev.setOnClickListener {
            supportFragmentManager.beginTransaction().apply {
                if (plate2.isVisible) {
                    replace(R.id.flfragment, plate1)
                }
                if (plate3.isVisible) {
                    replace(R.id.flfragment, plate2)
                    btnNext.text = "Next"
                }
                commit()
            }
        }

        btnNext.setOnClickListener {
            supportFragmentManager.beginTransaction().apply {
                if (plate1.isVisible) {
                    replace(R.id.flfragment, plate2)
                }
                if (plate2.isVisible) {
                    replace(R.id.flfragment, plate3)
                    btnNext.text = "Finish"
                }
                commit()
            }
        }

    }
}

xml 布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity">

    <Button
        android:id="@+id/btnPrev"
        android:layout_width="129dp"
        android:layout_height="45dp"
        android:text="Previous" />

    <Button
        android:id="@+id/btnNext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Next" />

    <FrameLayout
        android:id="@+id/flfragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

所有片段都有这个代码:

package com.example.ishihara_test

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText

/**
 * A simple [Fragment] subclass.
 * Use the [Frag_Plate2.newInstance] factory method to
 * create an instance of this fragment.
 */
class Frag_Plate2 : Fragment(R.layout.frag_plate2) {
    
}

片段的xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".Frag_Plate2">

    <!-- TODO: Update blank fragment layout -->

    <TextView
        android:id="@+id/plate2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="What is the number on the plate?"
        android:textSize="20sp" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="395dp"
        android:layout_height="wrap_content"
        android:src="@drawable/plate2" />

    <EditText
        android:id="@+id/editTextTextAnswer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLines="1"
        android:inputType="number"
        android:hint="Write number here..." />

</RelativeLayout>

我更喜欢 kotlin,但 java 代码也可以理解。
注意:我没有放代码 sn-ps,因为我上面说的更清楚,但如果有帮助,我会放。

非常感谢任何帮助,非常感谢:)

【问题讨论】:

  • 您能分享您的 Parent Activity 和 Fragment 类的代码吗?
  • @PiyushSatija 这些是主要活动的代码和 FrameLayout 中的示例片段,我的结果活动仍然是空的。

标签: android android-studio kotlin android-fragments android-activity


【解决方案1】:

要在一个片段上保存答案,然后在每个片段中重复此步骤,最好的方法是创建一个 ArrayList(of Answers),在每个片段事务之前将答案添加到这个 arraylist 并将这个 arraylist 发送到下一个片段和重复此操作直到最后一个片段,在最后一个片段上将您的答案放入此数组列表中,然后进一步将其传递给您的第二个活动。

在第二个活动上尝试在 Arraylist 上循环并在相关变量中获得所有答案。

第二种解决方案是创建一个抽象类,即 Friend.java / Friend.kt 将其扩展到您的基本活动中, 创建一个从片段接收​​答案并将其添加到arraylist(在朋友类中创建)的函数,

Friend.AnswerList(answer)

您可以在您的第二个活动中从 Friend Class 中获取此数组列表。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-23
    • 2015-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-10
    相关资源
    最近更新 更多