实施的DatePicker在片段 [英] Implementing DatePicker in Fragment

查看:126
本文介绍了实施的DatePicker在片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我'使用片段是片段并没有DialogFragment。

实例

我做了谷歌大部分的搜索结果展示了如何使用DialogFragment拥有的DatePicker。

由于片段和DialogFragment的类型不匹配

这是不工作在我的情况

任何例子或者想法将是有益的。

下面是片段的Java code

 公共类CreateReportFragment扩展片段{

        公共CreateReportFragment(){}

        公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState)
        {

               rootView = inflater.inflate(R.layout.activity_create_report,集装箱,假);
               initViews();
               返回rootView;
        }

        私人无效initViews()
       {

         最后的日历C = Calendar.getInstance();
             年= c.get(Calendar.YEAR);
             月= c.get(Calendar.MONTH);
             天= c.get(Calendar.DAY_OF_MONTH);

             editTextDate.setText(新的StringBuilder()
             //月第一个值为0,只需添加1
             .append(年)
             。附加(-)
                    .append(月+ 1)
                     。.append( - )追加(天));

        buttonDate =(按钮)rootView.findViewById(R.id.buttonDate);
        }
 

如何实施的DatePicker 片段

解决方案

使用 DialogFragment

如果我猜测的权利,你要显示的点击buttonDate的一个datepicker

<一个href="http://developer.android.com/guide/topics/ui/controls/pickers.html">http://developer.android.com/guide/topics/ui/controls/pickers.html

在按钮点击

  DialogFragment选择器=新DatePickerFragment();
picker.show(getFragmentManager(),日期选择器);
 

DatePickerFragment.java

 公共类DatePickerFragment扩展DialogFragment
实现DatePickerDialog.OnDateSetListener {


@覆盖
公共对话onCreateDialog(包savedInstanceState){
//使用当前日期作为选择器的默认日期
最后的日历C = Calendar.getInstance();
INT年= c.get(Calendar.YEAR);
INT月= c.get(Calendar.MONTH);
INT天= c.get(Calendar.DAY_OF_MONTH);

//创建DatePickerDialog的新实例,并将其返回
返回新DatePickerDialog(getActivity(),这,年,月,日);
}

@覆盖
公共无效onDateSet(DatePicker的观点,年整型,INT月,日整型){
日历C = Calendar.getInstance();
c.set(年,月,日);

SimpleDateFormat的SDF =新的SimpleDateFormat(YYYY-MM-DD);
串formattedDate = sdf.format(c.getTime());
}
}
 

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

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

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

Any example or idea would be helpful

Here is the Fragment Java Code

        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 DatePicker in Fragment?

解决方案

Use a DialogFragment

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

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

On Button click

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

DatePickerFragment.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());    
}
}

这篇关于实施的DatePicker在片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆