从DatePicker Button获取值时遇到困难?

时间:2014-07-21 05:15:37

标签: android datepicker

我有一个显示DatePicker的按钮。当我单击Button时,它将显示DatePickerDialog。我试图获取日期值,我想在TextView中获取它。但是当我试图获取价值时,我遇到了困难。如何在TextView中打印日期值?

这是我的代码。

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text1=(EditText) findViewById(R.id.editText1);
text2=(EditText) findViewById(R.id.editText2);
text3=(EditText) findViewById(R.id.editText3);
text4=(EditText) findViewById(R.id.editText4);
date=(Button) findViewById(R.id.datepicker);
date.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        DatePickerDialog dpd = new DatePickerDialog(MainActivity.this, mDateSetListener, myear, mmonth, mday);
        dpd.show();
    }
});
   final Calendar c = Calendar.getInstance();
   myear = c.get(Calendar.YEAR);
   mmonth = c.get(Calendar.MONTH);
   mday = c.get(Calendar.DAY_OF_MONTH);
}
private void updateDisplay() {
    // TODO Auto-generated method stub
    textdate.setText(new StringBuilder()
    .append(mmonth+1).append("-").append(mday).append("-")
    .append(myear));
}
private DatePickerDialog.OnDateSetListener mDateSetListener = new 
        DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                    int dayOfMonth) {
                myear=year;
                mmonth=monthOfYear;
                mday=dayOfMonth;
                updateDisplay();
            }
        };

3 个答案:

答案 0 :(得分:0)

在您要显示选择器的任何按钮上调用此选项

    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getSupportFragmentManager(), "datePicker");

显示选择器后,您将有ok按钮

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

        DatePickerDialog d = new DatePickerDialog(getActivity(), this,
                year, month, day);
        DatePicker dp = d.getDatePicker();
        dp.setMaxDate(c.getTimeInMillis());
        // Create a new instance of DatePickerDialog and return it
        return d;
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        // Do something with the date chosen by the user

        tv1.setText(day + "-" + month + "-" + year);

    }
}

在日期设定方法上参考textview

答案 1 :(得分:0)

本准则可以帮助您解决问题...

试试这个...

private DatePickerDialog.OnDateSetListener pickerListener = new DatePickerDialog.OnDateSetListener() {
    // when dialog box is closed, below method will be called.
    @SuppressLint("SimpleDateFormat")
    @Override
    public void onDateSet(DatePicker view, int selectedYear,
            int selectedMonth, int selectedDay) {
        pYear  = selectedYear;
        pMonth = selectedMonth;
        pDay   = selectedDay;

        // Show selected date 
        SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy");
        // saving date to String variable
        String formatedDate = sdf.format(new java.util.Date(pYear-1900, pMonth, pDay)); 
        // setting date to TextView


        // **Note :** Make sure that you have defined value to your textview i.e. textview must not null...
        txt_date.setText(formatedDate);

    }
};

答案 2 :(得分:0)

您尚未初始化textdate

创建一个全局变量textdate并将其初始化为

textdate = (TextView) findViewById(R.id.text_view);

其中text_view是您id的{​​{1}},就像您为TextViewEditText

所做的那样