微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 Fragment Android 中显示 DatePickerFragment

如何解决在 Fragment Android 中显示 DatePickerFragment

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

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

顺便说一下,我正在使用导航图在片段之间切换。

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

我的 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>

这是开发者网站上的解释。即使按照所描述的所有内容,我也没有获得所需输出的原因是什么? 请帮忙。

解决方法

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

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

,

您使用了 Activity 中可用的 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.
        }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?