如何传输的格式的日期字符串从我DatePickerFragment? [英] How to transfer the formatted date string from my DatePickerFragment?

查看:99
本文介绍了如何传输的格式的日期字符串从我DatePickerFragment?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

继官方文档<一href="http://developer.android.com/guide/topics/ui/controls/pickers.html#DatePicker">http://developer.android.com/guide/topics/ui/controls/pickers.html#DatePicker.

我已经用一样的code,并添加只有结果在 onDateSet 方法格式化:

I have used just the same code, and added only the result formatting in the onDateSet method:

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 并调用选择器,当用户触摸控件:

For testing, the caller activity just displays the TextView and calls the picker when the user touches the widget:

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

日期选择器显示,并设置断点,其中结果被格式化为一个字符串的地方后,我可以看到它的工作原理。

The date picker is displayed, and after setting the breakpoint at the place where the result is formatted to a string, I can see it works.

不过,我不知道如何通过字符串值回 mtDTDelivery 小工具?

However, I do not know how to pass the string value back to the mtDTDelivery widget?

推荐答案

由于tyczj指出,你可以使用该接口的方法,或者你可以让datapicker类的内部类的活动类的这种情况下,它应该是静态的,你需要该outerclass的WeakReference的

As tyczj pointed out you can use the interface method or you could make datapicker class an inner class of your activity class in which case it should be static and you need a weakreference of the outerclass

使用接口​​。

定义中的 DatePickerFragment A接口。实现该接口在您的活动类,并设置日期为TextView的。

Define a interface in DatePickerFragment. Implement the interface in your activity class and set the date to 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); 

}

}
}

MainActivity

MainActivity

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

}

第二种方法不知道这是最好的方式。我会建议使用上述接口方法。

Second way not sure if this is the best way. I would recommend the above interface method.

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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