我需要将DialogFragment放入片段中

时间:2019-05-15 04:32:18

标签: android android-fragments callback timepickerdialog

又是我。我正在做一个学校项目,并且我的Android应用程序获得了一些帮助。我需要在片段中放置一个TimePickerDialog,在这个片段中我有一个TextView,我想在此处选择小时。但是我找不到办法。当我做监听器时,它不起作用。

我试图将getActivity更改为可以解决的另一件事,但是它不起作用。

return new TimePickerDialog(getActivity(), (TimePickerDialog.OnTimeSetListener) getActivity(), hour, minute,
                DateFormat.is24HourFormat(getActivity()));

这是我的主要片段XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"

    tools:context=".DespertarFragment">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="83dp"
        android:layout_marginEnd="83dp"
        android:text="Seleccione la hora"
        android:textSize="30sp"
        app:layout_constraintBottom_toTopOf="@+id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="161dp"
        android:layout_marginTop="1dp"
        android:layout_marginEnd="162dp"
        android:text="Poner"
        app:layout_constraintBottom_toTopOf="@+id/button_cancel"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView1" />

    <Button
        android:id="@+id/button_cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="161dp"
        android:layout_marginTop="145dp"
        android:layout_marginEnd="162dp"
        android:layout_marginBottom="156dp"
        android:text="Cancelar"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button" />

</android.support.constraint.ConstraintLayout>

这是课程:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_despertar, container, false);
        Button poner = (Button) view.findViewById(R.id.button);
        poner.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogFragment timePicker = new TimePickerFragment();
                timePicker.show(getFragmentManager(), "Timepo");
            }
        });
        return view;
    }

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        TextView textView = (TextView) view.findViewById(R.id.textView1);
        textView.setText("Hora: " + hourOfDay + minute);
    }

这是我的TimePickerDialog类:

public class TimePickerFragment extends DialogFragment {
    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);
        return new TimePickerDialog(getActivity(), HERE I DON'T KNOW WHAT CAN I PUT HERE TO PASS THE TIME TO MY MAIN FRAGMENT, hour, minute,
                DateFormat.is24HourFormat(getActivity()));
    }
}

注意:我不需要TimePickerDialog类中的onTimeSet,我需要主片段中的时间。

我希望ViewText有时间,但是它什么也没变。

2 个答案:

答案 0 :(得分:0)

在TimePickerFragment中,将onCreateDialog()的代码放在onCreateView中。

答案 1 :(得分:0)

尝试一下,这是显示时间选择器的简单方法

 private void timePick(final TextView textView) {
    TimePickerDialog.OnTimeSetListener timer = new 
    TimePickerDialog.OnTimeSetListener() {
        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
           //set time in your text view
        }
    };

    new TimePickerDialog(this,timer,currenthour,currentMinuts,false).show();

}

调用此方法

 poner.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             timePick(yourextView);
        }
    });