检索和设置从DialogFragment数据(的DatePicker) [英] Retrieve and set data from DialogFragment (DatePicker)

查看:121
本文介绍了检索和设置从DialogFragment数据(的DatePicker)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的DialogFragment类被调用时,我preSS上的活动按钮。我想日通过这一DialogFragment设置为上的按钮显示的文本。 FindViewById拒绝被由类认可。

我发现了一个类似的问题但我无法把它与我的情况。

code:

  1. 按钮它调用对话片段:

     公共无效的日期选择器(视图v){
        DialogFragment newFragment =新DatePickerFragment();
        newFragment.show(getFragmentManager(),日期选择器);
    }
     

  2. code对话片段:

     公共类DatePickerFragment扩展DialogFragment工具
        DatePickerDialog.OnDateSetListener {
    
        @覆盖
        公共对话onCreateDialog(包savedInstanceSateate){
    
            最后的日历C = Calendar.getInstance();
                INT年= c.get(Calendar.YEAR);
                INT月= c.get(Calendar.MONTH);
                INT天= c.get(Calendar.DAY_OF_MONTH);
    
                返回新DatePickerDialog(getActivity(),这,年,月,日);
            }
    
            公共无效onDateSet(DatePicker的观点,年整型,INT月,日整型){
                //做一些与所选择的日期
            }
        }
    }
     

解决方案

在你的DialogFragment的方法是主管当用户设置的日期被通知,做到这一点

 按钮activityButton =(按钮)getActivity()findViewById(R.id.myButton)。
activityButton.setText(指明MyDate);
 

My DialogFragment class is invoked when I press a button on a activity. I want the date set through this DialogFragment to be displayed on the buttons text. FindViewById refuses to be recognized by the class.

I found a similar question but I'm unable to relate it to my case.

Code:

  1. Button which calls the Dialogue fragment:

    public void datepicker(View v) {
        DialogFragment newFragment = new DatePickerFragment();
        newFragment.show(getFragmentManager(), "datePicker"); 
    }
    

  2. Code for dialogue 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
            }
        }
    }
    

解决方案

In the method in your DialogFragment that is in charge of being notified when the user sets the date, do this

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

这篇关于检索和设置从DialogFragment数据(的DatePicker)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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