Android 在 Fragment 中实现 DatePicker

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20923604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:41:56  来源:igfitidea点击:

Implementing DatePicker in Fragment

androidandroid-fragmentsdatepickerfragment

提问by AndroidNewbie

I'am using Fragment which is an instance of Fragment and not of DialogFragment.

我正在使用 Fragment,它是 Fragment 的一个实例,而不是 DialogFragment 的实例。

I did google most of the search result shows how to use DialogFragment to have DatePicker.

我确实用谷歌搜索了大部分搜索结果,显示了如何使用 DialogFragment 来拥有 DatePicker。

which isn't working in my case because of type mismatch of Fragment and DialogFragment

由于 Fragment 和 DialogFragment 的类型不匹配,这在我的情况下不起作用

Any example or idea would be helpful

任何示例或想法都会有所帮助

Here is the Fragment Java Code

这是片段 Java 代码

        public class CreateReportFragment extends Fragment {

        public CreateReportFragment(){}

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

               rootView = inflater.inflate(R.layout.activity_create_report, container, false);
               initViews();
               return rootView;
        }

        private void initViews() 
       {

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

             editTextDate.setText(new StringBuilder()
             // Month is 0 based, just add 1
             .append(year)
             .append("-")
                    .append(month + 1)
                     .append("-").append(day));

        buttonDate = (Button)rootView.findViewById(R.id.buttonDate);
        }

How to implement DatePickerin Fragment?

如何在Fragment 中实现DatePicker

回答by Raghunandan

Use a DialogFragment

用一个 DialogFragment

If i am guessing right you want to show a DatePicker on click of buttonDate

如果我猜对了,您想在单击 buttonDate 时显示 DatePicker

http://developer.android.com/guide/topics/ui/controls/pickers.html

http://developer.android.com/guide/topics/ui/controls/pickers.html

On Button click

在按钮上单击

DialogFragment picker = new DatePickerFragment();
picker.show(getFragmentManager(), "datePicker");

DatePickerFragment.java

日期选择器片段.java

public class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {


@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
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); 

// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}

@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
Calendar c = Calendar.getInstance();
c.set(year, month, day);

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = sdf.format(c.getTime());    
}
}

回答by CommonsWare

How to implement DatePicker in Fragment?

如何在 Fragment 中实现 DatePicker?

The same way you would "implement" a TextViewin a Fragment: have onCreateView()return a Viewthat is or contains a DatePicker. For example, you might have a layout file that contains a DatePickerwidget, and have onCreateView()inflate that layout.

与您TextView在 a 中“实现” a 的方式相同FragmentonCreateView()返回 aView是或包含 a DatePicker。例如,您可能有一个包含DatePicker小部件的布局文件,并且onCreateView()对该布局进行了膨胀。

回答by Ojonugwa Jude Ochalifu

. . . which isn't working in my case because of type mismatch of Fragment and DialogFragment

. . . 由于 Fragment 和 DialogFragment 的类型不匹配,这在我的情况下不起作用

A DialogFragmentIS-A Fragmentbecause it extends Fragment.Therefore, you can use a DialogFragment anywhere you would a Fragment.

A DialogFragmentIS-AFragment因为它扩展了 Fragment。因此,您可以在任何需要使用 Fragment 的地方使用 DialogFragment。