显示Datepicker对话框,以线性布局扩展而不是活动

时间:2014-11-20 05:20:14

标签: android android-layout datepicker

我使用扩展线性布局显示UI而非以编程方式显示活动。比如

  

公共类DisplayDatePicker扩展了LinearLayout {

     

这是我想通过点击按钮getDate显示日期选择器;和   在textview上显示。 }

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?

此处显示showDialog框的详细信息:

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


        }

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

1 个答案:

答案 0 :(得分:2)

这对我有用。希望它也能帮助你

我已添加所有代码以实现此功能

将其保存在扩展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
}