我使用的是扩展线性布局而不是从程序上显示的活动。
公共类displaydatepicker扩展了linearlayout{
这里是我想通过点击按钮getdate来显示日期选择器;并且
在文本视图上显示。}

Date = Calendar.getInstance();

    btnnTest.setOnClickListener(new Button.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                 showDateDialog(etTest, Date);
                            }

                        });

现在展开上面的函数ShowDateDialog,它调用成功但没有显示DatePicker对话框;
protected void showDateDialog(EditText etLocationTest2, Calendar date2) {
        Log.i("", "Date showDateDialog ");
     ((Activity) getContext()).showDialog(DATE_DIALOG_ID);

    }

In here Could not display Date picker Dialog box, why? i think showDialog box could not call DATE_DIALOG_ID,
so how to call this, any idea?

此处显示对话框的截止日期:
protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
            // set date picker as current date
                    return new DatePickerDialog(getContext(), datePickerListener, year, month,day);
        return null;
    }



private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
        // when dialog box is closed, below method will be called.
        public void onDateSet(DatePicker view, int selectedYear,
                int selectedMonth, int selectedDay) {
            year = selectedYear;
            month = selectedMonth;
            day = selectedDay;
            // set selected date into textview
            tvDisplayDate.setText(new StringBuilder().append(month + 1)
                    .append("-").append(day).append("-").append(year)
                    .append(" "));


        }

我希望你理解我的问题,并等待任何回应。

最佳答案

这对我有效。希望对你也有帮助
我已经在下面添加了实现此功能的所有代码
将其保存在扩展linearlayout的类中

DatePopListener mDatePopListener;
private DatePickerDialog datePickerDialog;
private SimpleDateFormat dateFormatter;

创建一个构造函数,从中获取初始化该类的上下文。
使用的代码对我来说很好。
class displayDatePicker extends linearlayout{
DatePopListener mDatePopListener;
EditText edittext;
TextBox label;
private DatePickerDialog datePickerDialog;
private SimpleDateFormat dateFormatter;
public  displayDatePicker(Context context,String labelText){
dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
Calendar newCalendar = Calendar.getInstance();
label.setText(labelText);
this.addView(label);
this.addView(txtBox);
txtBox.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
          datePickerDialog.show();

        }
    });
    datePickerDialog = new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {

        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            Calendar newDate = Calendar.getInstance();
            newDate.set(year, monthOfYear, dayOfMonth);
            txtBox.setText(dateFormatter.format(newDate.getTime()));
        }

    },newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));


}
//Now you can do your task here whatever as the date will be available in txtBox
//Enter your code as your requirements
}

07-28 01:27
查看更多