【问题标题】:Show DatePickerFragment in a Fragment Android在 Fragment Android 中显示 DatePicker 片段
【发布时间】:2021-01-05 19:41:09
【问题描述】:

我花时间搜索找到可行的解决方案。有些不适用于我的情况,有些不起作用。这就是我在这里发布此内容的原因。

我正在尝试使用片段中的 DatePickerFragment 选择日期。我按照 Android 开发者网站 here 中的说明进行操作。即使执行了所有步骤,我也没有编译代码。

顺便说一下,我正在使用 Navigation Graph 在片段之间切换。

这是我所做的以及我遇到的问题:

我的 DatePickerFragment

class DatePickerFragment : DialogFragment(), DatePickerDialog.OnDateSetListener {

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val c = Calendar.getInstance()
        val year = c.get(Calendar.YEAR)
        val month = c.get(Calendar.MONTH)
        val dayOfMonth = c.get(Calendar.DAY_OF_MONTH)
        return DatePickerDialog(requireActivity(), this, year, month, dayOfMonth)
    }

    override fun onDateSet(view: DatePicker?, year: Int, month: Int, day: Int) {
        // Do something with the date chosen by the user

        // I want to update the button text of my AddNewTransactionFragment with the date picked
    }
}

我的 AddNewTransactionFragment

supportFragmentManager 无法识别。

class AddNewTransactionFragment : Fragment(R.layout.fragment_add_new_transaction) {

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        fun showDatePickerDialog(v: View) {
            val newFragment = DatePickerFragment()
            newFragment.show(supportFragmentManager, "datePicker")

            // here this 'supportFragmentManager' is red and the code won't compile with the
            // error: Unresolved reference: supportFragmentManager.
            // and the function showDatePickerDialog is grayed out even though I have
            // android:onClick="showDatePickerDialog" in the corresponding xml file.
        }
    }
}

我的片段 AddNewTransactionFragment 的 xml 文件

这里的按钮 android:onClick= 属性有一个方法但是它是红色的,从 Fragment 代码中无法识别。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
    android:background="@color/backgroundGray"
    android:padding="25dp"
    tools:context="com.sukajee.splitexpense.AddNewTransactionFragment">


    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/textViewAddNew"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingVertical="10dp"
            android:text="Add New Transaction"
            android:textColor="@color/colorAccent"
            android:textSize="20sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/textViewTransactionDate"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="10dp"
            android:text="Date"
            android:textColor="@color/colorAccent"
            android:textSize="15sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/textViewAddNew" />

        <Button
            android:id="@+id/buttonTransactionDate"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Friday, 01 January 2021"
            app:layout_constraintStart_toStartOf="parent"
            android:onClick="showDatePickerDialog"
            app:layout_constraintTop_toBottomOf="@id/textViewTransactionDate" />

        <EditText
            android:id="@+id/editTextDescription"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:ellipsize="end"
            android:hint="Description (Grocery, Food, Laundry etc.)"
            android:maxLines="1"
            android:textColorHint="@color/colorAccent"
            android:textSize="15sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/buttonTransactionDate" />

        <EditText
            android:id="@+id/editTextStore"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:ellipsize="end"
            android:hint="Store Name"
            android:maxLines="1"
            android:textColorHint="@color/colorAccent"
            android:textSize="15sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/editTextDescription" />

        <EditText
            android:id="@+id/editTextAmount"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:ellipsize="end"
            android:hint="Amount"
            android:inputType="numberDecimal"
            android:maxLines="1"
            android:textColorHint="@color/colorAccent"
            android:textSize="15sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/editTextStore" />

        <EditText
            android:id="@+id/editTextNote"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:ellipsize="end"
            android:hint="Note"
            android:inputType="textMultiLine"
            android:textColorHint="@color/colorAccent"
            android:textSize="15sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/editTextAmount" />

        <Button
            android:id="@+id/buttonSubmit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:text="Submit"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/editTextNote" />

        <Button
            android:id="@+id/buttonCancel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:text="Cancel"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/buttonSubmit" />

        <Button
            android:id="@+id/buttonClear"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:text="Clear"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/buttonCancel" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

这就是开发者网站中的解释。即使遵循了所描述的一切,我也没有得到所需输出的原因可能是什么? 请帮忙。

【问题讨论】:

    标签: android android-fragments android-datepicker android-navigation-graph


    【解决方案1】:

    您正在尝试从 Fragment 使用 supportFragmentManager,但 supportFragmentManager 只能从 Activity 尤其是 AppCompatActivity 或 FragmentActivity 中使用。

    您可以从 Fragment 将 childFragmentManager 用作 FragmentManager,但我从未尝试从 Fragment 打开 DialogFragment。

    【讨论】:

      【解决方案2】:

      您使用了活动中可用的supportFragmentManager,并且您在另一个方法中编写了一个方法。 试试这个。

      class AddNewTransactionFragment : Fragment(R.layout.fragment_add_new_transaction) {
      
          override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
              super.onViewCreated(view, savedInstanceState)
              someButton.setOnClickListener{v->showDatePickerDialog(v)}
          }
      
      fun showDatePickerDialog(v: View) {
                  val newFragment = DatePickerFragment()
                  newFragment.show(childFragmentManager, "datePicker")
      
                  // here this 'childFragmentManager' is red and the code won't compile with the
                  // error: Unresolved reference: supportFragmentManager.
                  // and the function showDatePickerDialog is grayed out even though I have
                  // android:onClick="showDatePickerDialog" in the corresponding xml file.
              }
      }
      

      【讨论】:

      • 使用 childFragmentManager,我可以看到 DatePickerFragment。我仍然对 DatePickerFragment 中 onDateSet 方法的实现感到困惑。如何通过此方法访问其他片段的按钮并设置文本?
      • 经过数小时的尝试和研究,我找到了访问其他片段按钮的方法。我刚刚使用了 parentFragment?.view?.findViewById
      猜你喜欢
      • 2015-01-29
      • 1970-01-01
      • 2015-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-15
      • 2015-07-19
      • 2012-12-30
      相关资源
      最近更新 更多