我需要将 DialogFragment 放在 Fragment 中

南木

又是我。我正在做一个学校项目,我的 Android 应用程序得到了一些帮助。我需要在 Fragment 中放置一个 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 但它什么也没改变。

纳文

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

 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);
        }
    });

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

需要将罐子放在我的Maven本地存储库中

来自分类Dev

从DialogFragment更新Fragment中的ListView

来自分类Dev

我是否需要将所有内容都放在C ++中的类中?

来自分类Dev

我有多个时间序列,需要将它们放在一个data.frame中

来自分类Dev

在Fragment的onDestroy()中是否需要将ViewBinding设置为null?

来自分类Dev

我需要将 dict 中的所有键和值都放在列表中,但只返回最后一个键和值

来自分类Dev

从未从Fragment中的DialogFragment调用Android onActivityResult

来自分类Dev

将SupportMapFragment放在DialogFragment上

来自分类Dev

为什么需要将Python中的future_statements放在所有内容的前面?

来自分类Dev

如果要隐藏{{xxx}}使其不显示在页面上,是否需要将AngularJS放在<head>中?

来自分类Dev

需要将包含img url的Json链接放在<img ng-src中

来自分类Dev

我被教导不要将大多数方法放在通用的“系统”类中,而是将它们放在哪里?

来自分类Dev

将代码放在 Activity 或 Fragment 中以处理方向更改?

来自分类Dev

我需要将 VS SSDT 和 VS 放在一个地方,我将它们分开

来自分类Dev

为什么我需要将“ do”与“ for”放在同一行?

来自分类Dev

为什么我们需要将 const 放在函数 GetMax 的开头?

来自分类Dev

为什么在使用循环时需要将带有键的字典名称放在附加括号中?

来自分类Dev

我需要将最大的物体留在图像中

来自分类Dev

我需要将for循环的结果保存到数据框中

来自分类Dev

我需要将表行包装到div中

来自分类Dev

我需要将数据添加到数组中

来自分类Dev

在Ruby中,我需要将句子拆分为子句

来自分类Dev

我需要将数组值转换为jQuery中的json

来自分类Dev

如果放在外部机箱中,我需要格式化我的硬盘吗?

来自分类Dev

如何从DialogFragment到Fragment获取数据?

来自分类Dev

将数据从DialogFragment发送到Fragment

来自分类Dev

是否需要将aria标签放在表单提交按钮上?

来自分类Dev

我的自定义DialogFragment没有显示在活动中

来自分类Dev

如何在我的 DialogFragment 中显示进度条?

Related 相关文章

  1. 1

    需要将罐子放在我的Maven本地存储库中

  2. 2

    从DialogFragment更新Fragment中的ListView

  3. 3

    我是否需要将所有内容都放在C ++中的类中?

  4. 4

    我有多个时间序列,需要将它们放在一个data.frame中

  5. 5

    在Fragment的onDestroy()中是否需要将ViewBinding设置为null?

  6. 6

    我需要将 dict 中的所有键和值都放在列表中,但只返回最后一个键和值

  7. 7

    从未从Fragment中的DialogFragment调用Android onActivityResult

  8. 8

    将SupportMapFragment放在DialogFragment上

  9. 9

    为什么需要将Python中的future_statements放在所有内容的前面?

  10. 10

    如果要隐藏{{xxx}}使其不显示在页面上,是否需要将AngularJS放在<head>中?

  11. 11

    需要将包含img url的Json链接放在<img ng-src中

  12. 12

    我被教导不要将大多数方法放在通用的“系统”类中,而是将它们放在哪里?

  13. 13

    将代码放在 Activity 或 Fragment 中以处理方向更改?

  14. 14

    我需要将 VS SSDT 和 VS 放在一个地方,我将它们分开

  15. 15

    为什么我需要将“ do”与“ for”放在同一行?

  16. 16

    为什么我们需要将 const 放在函数 GetMax 的开头?

  17. 17

    为什么在使用循环时需要将带有键的字典名称放在附加括号中?

  18. 18

    我需要将最大的物体留在图像中

  19. 19

    我需要将for循环的结果保存到数据框中

  20. 20

    我需要将表行包装到div中

  21. 21

    我需要将数据添加到数组中

  22. 22

    在Ruby中,我需要将句子拆分为子句

  23. 23

    我需要将数组值转换为jQuery中的json

  24. 24

    如果放在外部机箱中,我需要格式化我的硬盘吗?

  25. 25

    如何从DialogFragment到Fragment获取数据?

  26. 26

    将数据从DialogFragment发送到Fragment

  27. 27

    是否需要将aria标签放在表单提交按钮上?

  28. 28

    我的自定义DialogFragment没有显示在活动中

  29. 29

    如何在我的 DialogFragment 中显示进度条?

热门标签

归档