从datepickdialog获取选定日期的正确方法,它扩展了片段

时间:2014-03-08 15:08:14

标签: android android-datepicker

我正在关注DatePickerDialog上的android教程。我能够显示datepickerdialog并设置所选日期。我设置所选日期的方法是创建EditTextview的静态实例并从DatePickerFragment类访问它。这是设置所选日期的正确方法吗?

public class NewAssignment extends FragmentActivity {
       public static EditText textViewDatePicker;   //static 
protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.new_assignment);
        textViewDatePicker= (EditText) findViewById(R.id.datePicker);

    }


public void showDatePickerDialog(View v) {
        DialogFragment newFragment = new DatePickerFragment();
        newFragment.show(getSupportFragmentManager(), "datePicker");

    }


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

    public void onDateSet(DatePicker view, int year, int month, int day) {
        // Do something with the date chosen by the user
         NewAssignment.textViewDatePicker.setText("year: "+year+ "month: "+month+ "day: "+day);
    }
}

2 个答案:

答案 0 :(得分:1)

实际上,您可以在NewAssignment Activity中创建并显示DatePickerDialog。在这种情况下,DataPickerFragment是减少的。您也可以将OnDateSetListener分配给您的Activity。它可以是这样的:

 public class NewAssignment extends FragmentActivity implements
        DatePickerDialog.OnDateSetListener {
    private EditText textViewDatePicker;

    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textViewDatePicker = (EditText) findViewById(R.id.angle_value);
        showDatePickerDialog();
    }


    public void showDatePickerDialog() {
        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
        DatePickerDialog datePickerDialog = new DatePickerDialog(this, this, year, month, day);
datePickerDialog.show();
    }

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        textViewDatePicker.setText("year: " + year + "month: " + monthOfYear + "day: " + dayOfMonth);
    }
}

答案 1 :(得分:0)

您应该能够使用getActivity()。findViewById(R.id.datePicker)来获取片段中的编辑文本。

或者您可以在DatePickerFragment中覆盖onAttach(activity),然后将EditText字段设置为activity.findViewById(R.id.datePicker)