为了账号安全,请及时绑定邮箱和手机立即绑定

如何从我的DatePickerFragment传输格式化的日期字符串?

/ 猿问

如何从我的DatePickerFragment传输格式化的日期字符串?

开满天机 2019-12-06 11:19:47

遵循官方文档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小部件?


查看完整描述

3 回答

?
互换的青春

正如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);


    }

    }

}


查看完整回答
反对 回复 2019-12-06
?
Cats萌萌

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



查看完整回答
反对 回复 2019-12-06
?
缥缈止盈

您可以在中定义接口DatePickerFragment。然后在您的活动类中实现该接口。您将值设置为接口方法。由于您的活动类实现了接口,因此您可以在那里获得值。检查主题创建到活动 developer.android.com/guide/components/…的事件的回调

查看完整回答
反对 回复 2019-12-06

添加回答

回复

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信