android - 在线性布局中扩展显示日期选择器对话框而不是 Activity

标签 android android-layout datepicker

我正在使用扩展线性布局显示 UI,而不是以编程方式进行 Activity 。就像这样

public class DisplayDatePicker extends LinearLayout{

Here is i want to display date picker by click button getDate; and display on 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?

此处显示对话框的详细信息:

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


        }

希望您能理解我的问题,并等待任何回复。

最佳答案

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

我已经在下面添加了所有的代码来实现它

将其保存在扩展线性布局的类中

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
}

关于android - 在线性布局中扩展显示日期选择器对话框而不是 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27032313/

相关文章:

android如何检查服务是否正在运行

Android 日期选择器对话框

仅显示一周的 Javascript 日期选择器?

Android 应用程序移动到 sdcard 选项被禁用

java - Android:空指针异常

android - 根据条件查询获取行数

Android Swipe Tab Action Bar - 使选项卡中的更改持久化

java - TableRow 权重缩放与我在 Android java 应用程序中使用 GraphView 时的预期相反

java - Android 填屏不支持 S3 S4 屏幕分辨率

Jquery datepicker年份变化