如何从我的 DatePicker 片段中传输格式化的日期字符串?

Posted

技术标签:

【中文标题】如何从我的 DatePicker 片段中传输格式化的日期字符串?【英文标题】:How to transfer the formatted date string from my DatePickerFragment? 【发布时间】:2013-08-15 05:19:56 【问题描述】:

关注官方文档http://developer.android.com/guide/topics/ui/controls/pickers.html#DatePicker。

我只使用了相同的代码,并且只在onDateSet 方法中添加了结果格式:

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

        // How to get the string from here to the caller?

    

为了测试,调用者活动只显示TextView,并在用户触摸小部件时调用选择器:

public class OrderHeadEditActivity extends Activity 

    private TextView mDTDelivery;
    ...

    @Override
    protected void onCreate(Bundle bundle) 
        ...
        mDTDelivery = (TextView) findViewById(R.id.order_head_view_dt_delivery);
        ...
        mDTDelivery.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                DialogFragment picker = new DatePickerFragment();
                picker.show(getFragmentManager(), "datePicker");
            
        );
    
...

显示日期选择器,在结果格式化为字符串的地方设置断点后,我可以看到它起作用了。

但是,我不知道如何将字符串值传回mtDTDelivery 小部件?

【问题讨论】:

让你的数据选择器类成为你的活动类的内部类 您尝试过以下方法吗??接口方法将起作用。 我还没试过。我会在今天晚些时候尝试。我想先了解一下。 (我是 Java 及其 iterfaces 的新手,也是 Android 的新手。) 【参考方案1】:

正如 tyczj 指出的那样,您可以使用接口方法,或者您可以将 datapicker 类设为活动类的内部类,在这种情况下,它应该是静态的,并且您需要外部类的弱引用

使用界面。

DatePickerFragment 中定义一个接口。在您的活动类中实现接口并将日期设置为 textview。

public class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener 

    TheListener listener;

public interface TheListener
    public void returnDate(String date);


@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);
listener = (TheListener) getActivity(); 

// 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());
if (listener != null) 

  listener.returnDate(formattedDate); 





主活动

public class MainActivity extends Activity implements DatePickerFragment.TheListener

    Button b;
    TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b= (Button) findViewById(R.id.button1);
        tv= (TextView) findViewById(R.id.textView1);
        b.setOnClickListener(new OnClickListener()
        

            @Override
            public void onClick(View arg0) 
                // TODO Auto-generated method stub
                DialogFragment picker = new DatePickerFragment();
                picker.show(getFragmentManager(), "datePicker");
            

        );
    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    

    @Override
    public void returnDate(String date) 
        // TODO Auto-generated method stub
        tv.setText(date);
    


第二种方法不确定这是否是最好的方法。我会推荐上面的接口方法。

public class MainActivity extends Activity //implements DatePickerFragment.TheListener

    Button b;
    TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b= (Button) findViewById(R.id.button1);
        tv= (TextView) findViewById(R.id.textView1);
        b.setOnClickListener(new OnClickListener()
        

            @Override
            public void onClick(View arg0) 
                // TODO Auto-generated method stub
                DialogFragment picker = new DatePickerFragment();
                picker.show(getFragmentManager(), "datePicker");
            

        );
    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    


    public static class DatePickerFragment extends DialogFragment
    implements DatePickerDialog.OnDateSetListener 

        private WeakReference<MainActivity> mActivity;

    @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);
    mActivity = new WeakReference<MainActivity>((MainActivity) getActivity());

    // 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());
      MainActivity target = mActivity.get(); 
      if (target != null) target.tv.setText(formattedDate);

    
    

【讨论】:

我得再研究一下。无论如何,DatePickerFragment 中的listener 是如何获得其非空值的? 你在DatePickerFragment中定义了一个接口。然后在您的活动类中实现接口。您将值设置为接口方法。由于您的活动类实现了接口,因此您在那里获得了价值。检查主题为活动创建事件回调 developer.android.com/guide/components/… @pepr 了解接口。 docs.oracle.com/javase/tutorial/java/concepts/interface.html 谢谢,它有效。我之前没有注意到listener = (TheListener) getActivity(); :) 也感谢您的链接。 @Raghunandan 当我添加此行 listener = (TheListener) getActivity(); 并单击 EditTextBox 时,应用程序停止工作并自动关闭【参考方案2】:

正确的方法是为传递字符串的活动创建回调。

看看Communicating with an Activity

【讨论】:

以上是关于如何从我的 DatePicker 片段中传输格式化的日期字符串?的主要内容,如果未能解决你的问题,请参考以下文章

如何从我的活动中意图在我的标签栏中分段?

Angular bootstrap datepicker 日期格式不格式化 ng-model 值

试图弄清楚如何从 jquery datepicker 中删除假期

使用 Jquery Datepicker 获取日期

Datepicker JQUERY 不选择输入日期

有没有办法从我的 Dialog 片段中的 Activity 调用方法?