【问题标题】:How to get date from OnDateSetListener into a Fragment如何从 OnDateSetListener 获取日期到片段中
【发布时间】:2016-08-31 16:17:11
【问题描述】:

如何获取从DatePickerFragment 中选择的日期?

public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

    int Year,Month,Day;

    @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 monthOfYear, int dayOfMonth) {

    }
}

我的Fragment 我想在其中获取日期并将其设置为文本字段:

public class CalculatorHomeFragment extends Fragment

LinearLayout lyDate;
TextView lblDate;
int Year,Month,Day;

private static CalculatorHomeFragment instance = null;

public CalculatorHomeFragment() {

}

public static CalculatorHomeFragment getInstance() {
    if (instance == null)
        instance = new CalculatorHomeFragment();

    return instance;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_calculator_home, container, false);
    lyDate = (LinearLayout) rootView.findViewById(R.id.lyDate);
    lblDate = (TextView) rootView.findViewById(R.id.lblDate);
    lyDate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DialogFragment newFragment = new DatePickerFragment();
            newFragment.show(getActivity().getFragmentManager(), "datePicker");

        }
    });


    return rootView;
}

【问题讨论】:

  • 我在 DatePickerFragment 的 onDateSet 方法中获取日期,但我希望它在 CalculatorHomeFragment 的主要片段中。
  • 你需要添加activity和fragment之间的通信来更新值。

标签: android android-fragments datepicker


【解决方案1】:

你真的不需要第一个类来获取 DataPicker,只需像这样更改你的 onClickListener:

lyDate.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        final Calendar c = Calendar.getInstance();
    int mYear = c.get(Calendar.YEAR);
    int mMonth = c.get(Calendar.MONTH);
    int mDay = c.get(Calendar.DAY_OF_MONTH);
    DatePickerDialog dateDialog= new DatePickerDialog(getActivity(), datePickerListener, mYear, mMonth, mDay);
    dateDialog.show();

    }
});

然后像这样在你的类中添加一个 OnDateSetListener:

private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        String dateYouChoosed = dayOfMonth + "/" + (monthOfYear + 1) + "/" + year;
        lblDate.setText(dateYouChoosed );
    }
};

【讨论】:

  • 如何使用lblDate.getText().toString(); 获取lblDate.setText(dateYouChoosed ); 中设置的日期?
猜你喜欢
  • 1970-01-01
  • 2012-01-04
  • 2018-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多