android - 从 DialogFragment (DatePicker) 检索和设置数据

标签 android android-dialogfragment

我的 DialogFragment 类在我按下 Activity 按钮时被调用。我希望通过此 DialogFragment 设置的日期显示在按钮文本上。 FindViewById 拒绝被类识别。

我找到了一个 similar question但我无法将其与我的情况联系起来。

代码:

  1. 调用对话 fragment 的按钮:

    public void datepicker(View v) {
        DialogFragment newFragment = new DatePickerFragment();
        newFragment.show(getFragmentManager(), "datePicker"); 
    }
    
  2. 对话 fragment 代码:

    public class DatePickerFragment extends DialogFragment implements
        DatePickerDialog.OnDateSetListener {
    
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceSateate) {
    
            final Calendar c = Calendar.getInstance();
                int year = c.get(Calendar.YEAR);
                int month = c.get(Calendar.MONTH);
                int day = c.get(Calendar.DAY_OF_MONTH);
    
                return new DatePickerDialog(getActivity(), this, year, month, day);
            }
    
            public void onDateSet(DatePicker view, int year, int month, int day) {
                // Do something with the date chosen
            }
        }
    }
    

最佳答案

在您的 DialogFragment 中负责在用户设置日期时收到通知的方法中执行此操作

Button activityButton = (Button)getActivity().findViewById(R.id.myButton);
activityButton.setText (myDate);

关于android - 从 DialogFragment (DatePicker) 检索和设置数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14024921/

相关文章:

android - 从Kotlin Android中的Application变量创建单独的ArrayList

android - 防止未经授权访问 Restful API

Android 设计布局 ListItem RecyclerView Horizo​​ntal

android - 从 dialogfragment 发布新数据后更新 Listview

java - Android 中对话框的基类

android - TypeError 'undefined' 不是对象(评估 $ 事件)

Android使用OnPreDrawListener获取imageview的宽度和高度

android - getSupportFragmentManager() 未定义

android - 在android中的类中的对话框 fragment 的线性布局中添加textview

java - Android 中类似 facebook 的 react 按钮