DatePicker onDateSet 方法不是 运行

DatePicker onDateSet method not running

我是 android 工作室的完全初学者,我正在按照本教程从 DatePicker 小部件向我的应用插入日期。 https://www.youtube.com/watch?v=33BFCdL0Di0

此功能存在于我拥有的片段中,而不存在于我的 MainActivity 中。问题是当我单击日历上的日期时,onDateSet 方法不会 运行。

我的代码:

DatePickerFragment.java

public class DatePickerFragment extends DialogFragment {

    Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState){
       return new DatePickerDialog(getContext(),(DatePickerDialog.OnDateSetListener) getView() ,year,month,day);

    }

}

StatisticsFragment.java //我插入日期的地方

public class StatisticsFragment extends Fragment implements DatePickerDialog.OnDateSetListener{

    private FragmentStatisticsBinding binding;
    private View root;
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        binding = FragmentStatisticsBinding.inflate(inflater, container, false);
         root = binding.getRoot();

        EditText fromInput = (EditText) root.findViewById(R.id.inputFrom);
        fromInput.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogFragment datePicker = new DatePickerFragment();
                datePicker.show(requireActivity().getSupportFragmentManager(), "date picker");

            }
        });

        return root;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

    @Override
    public void onDateSet(DatePicker view , int year , int month , int dayOfMonth){
        System.out.println("I am in ");    // does not enter
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR,year);
        c.set(Calendar.MONTH,month);
        c.set(Calendar.DAY_OF_MONTH,dayOfMonth);
        String currentDateString = DateFormat.getDateInstance(DateFormat.FULL).format(c.getTime());

        TextView fromText =  root.findViewById(R.id.from);
        fromText.setText(currentDateString);
    }
}

frament_statistics.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">


    <TextView
        android:id="@+id/from"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="44dp"
        android:layout_marginTop="56dp"
        android:text="From :"
        android:textSize="20sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/viewStats" />




    <EditText
        android:id="@+id/inputTo"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="36dp"
        android:layout_marginTop="16dp"
        android:hint="Enter date"
        android:textAlignment="center"
        android:textSize="20sp"
        app:layout_constraintStart_toEndOf="@+id/inputFrom"
        app:layout_constraintTop_toBottomOf="@+id/to" />

    <TextView
        android:id="@+id/viewStats"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="40dp"
        android:layout_marginTop="64dp"
        android:text="View vaccination statistics for specific dates : "

        android:textSize="17sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

非常感谢你的帮助

您还没有将 DatePickerStatisticsFragment OnDateSetListener 连在一起。

(DatePickerDialog.OnDateSetListener) getView()

在这个生命周期阶段,DialogFragment 没有视图并且 getView() returns 为空。 null 可以转换为任何类型,因此这里也不例外。将 null 作为侦听器传递意味着没有侦听器。

您可以去掉 DatePickerFragment,直接在 SettingsFragment 中启动日期选择器,this 作为侦听器。

DatePickerDialog 是一个 AlertDialog,因此您将失去 DialogFragment 的一些好处,例如配置更改支持。)

我稍微修改了您的代码并使其工作。 请通过它。

StatisticsFragment.java

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import androidx.annotation.NonNull;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import java.text.DateFormat;
import java.util.Calendar;

public class StatisticsFragment extends Fragment implements DatePickerDialog.OnDateSetListener {

private FragmentStatisticsBinding binding;
private View root;

public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    binding = FragmentStatisticsBinding.inflate(inflater, container, false);
    root = binding.getRoot();

    binding.inputTo.setOnClickListener(v -> {
        DialogFragment datePicker = new DatePickerFragment(StatisticsFragment.this);
        datePicker.show(requireActivity().getSupportFragmentManager(), "date picker");
    });

    return root;
}

@Override
public void onDestroyView() {
    super.onDestroyView();
    binding = null;
}

@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
    System.out.println("I am in ");    // does not enter
    Calendar c = Calendar.getInstance();
    c.set(Calendar.YEAR, year);
    c.set(Calendar.MONTH, month);
    c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
    String currentDateString = DateFormat.getDateInstance(DateFormat.FULL).format(c.getTime());

    binding.from.setText("From : " + currentDateString);
}
}

DatePickerFragment.java

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.fragment.app.DialogFragment;
import java.util.Calendar;

public class DatePickerFragment extends DialogFragment {

Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog.OnDateSetListener listener;

public DatePickerFragment(DatePickerDialog.OnDateSetListener listener) {
    this.listener = listener;
}

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    return new DatePickerDialog(getContext(), listener, year, month, day);
}
}

fragment_statistics.xml

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_orange_light">

    <TextView
        android:id="@+id/from"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="44dp"
        android:layout_marginTop="56dp"
        android:text="From :"
        android:textSize="20sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/viewStats" />


    <EditText
        android:id="@+id/inputTo"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="36dp"
        android:layout_marginTop="16dp"
        android:focusable="false"
        android:hint="Select date"
        android:textAlignment="center"
        android:textSize="20sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/from" />

    <TextView
        android:id="@+id/viewStats"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="40dp"
        android:layout_marginTop="64dp"
        android:text="View vaccination statistics for specific dates : "
        android:textSize="17sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

希望这会有所帮助。