Datepicker对话框无法在android中显示

时间:2015-11-28 07:17:21

标签: android datepicker

我试图使用微调器小部件来获取日期。日期显示在TOAST消息上,但是当我单击spinner datepicker对话框时,不会打开。找不到错。

这是我的代码段。

主要活动。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_apply_leave);

    leaveApplyFromDate = (Spinner)findViewById(R.id.leaveApplyFromDate);

    calendar = Calendar.getInstance();
    year = calendar.get(Calendar.YEAR);

    month = calendar.get(Calendar.MONTH);
    day = calendar.get(Calendar.DAY_OF_MONTH);
    showDate(year, month+1, day);


    leaveApplyFromDate.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getBaseContext(), leaveApplyFromDate.getSelectedItem().toString(),
                    Toast.LENGTH_SHORT).show();
            setDate(view);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });


}

@Override
protected Dialog onCreateDialog(int id) {
    // TODO Auto-generated method stub
    if (id == 999) {
        return new DatePickerDialog(this, myDateListener, year, month, day);
    }
    return null;
}
@SuppressWarnings("deprecation")
public void setDate(View view) {
    showDialog(999);
    Toast.makeText(getApplicationContext(), "ca", Toast.LENGTH_SHORT)
            .show();
}

private DatePickerDialog.OnDateSetListener myDateListener = new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub
        // arg1 = year
        // arg2 = month
        // arg3 = day
        showDate(arg1, arg2+1, arg3);
    }
};

private void showDate(int year, int month, int day) {
   // leaveApplyFromDate.setText(new StringBuilder().append(day).append("/")
       //     .append(month).append("/").append(year));
    Toast.makeText(getBaseContext(), new StringBuilder().append(day).append("/")
                .append(month).append("/").append(year),
            Toast.LENGTH_SHORT).show();
}

这是我的xml布局。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.hrm.staff.ApplyLeave">

            <Spinner
            android:id="@+id/leaveApplyFromDate"
            android:layout_height="@dimen/spinner_height"
            android:layout_weight="0.65"
            android:background="@color/spinner_bg_start"
            android:ems="10" />


</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

您应该在活动的某个地方调用showDialog(999)

答案 1 :(得分:-1)

确定。现在我得到了答案。我已经改变了一些代码和xml以使其工作。希望这对其他人也有帮助。我在这里发布了正在运行的代码

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_apply_leave);

leaveApplyFromDate = (Spinner)findViewById(R.id.leaveApplyFromDate);

calendar = Calendar.getInstance();
year = calendar.get(Calendar.YEAR);

month = calendar.get(Calendar.MONTH);
day = calendar.get(Calendar.DAY_OF_MONTH);
showDate(year, month+1, day);

 setDate(); // calling this function to fetch the date before it is clicked

 /* applying onlicklistener to textview of the date to open the date dialog box */ 
 leaveApplyFromDate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            setDateDialog(); // calling this function to display date dialog box

        }

    });
leaveApplyFromDate.setText(fromDate); // setting date for textview on loading the app

}

@Override
protected Dialog onCreateDialog(int id) {
    // TODO Auto-generated method stub
    if (id == 999) {
        return new DatePickerDialog(ApplyLeave.this, myDateListener, year, month, day);
    }
    return null;
}

@SuppressWarnings("deprecation")
public void setDate() {


}

@SuppressWarnings("deprecation")
public void setDateDialog() {
    showDialog(999);

}

private DatePickerDialog.OnDateSetListener myDateListener = new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub
        // arg1 = year
        // arg2 = month
        // arg3 = day
        showDate(arg1, arg2 + 1, arg3);
        leaveApplyFromDate.setText(fromDate); // setting date for textview after selecting it from date widget.
    }
};

private void showDate(int year, int month, int day) {

    StringBuilder date = new StringBuilder().append(day).append("/")
            .append(month).append("/").append(year);
    fromDate = date.toString();

}

这是我的Xml布局。

 <TextView
            android:id="@+id/leaveApplyFromDate"
            android:layout_height="wrap_content"
            android:layout_weight="0.65"
            android:background="@drawable/bg_file"
            android:textSize="@dimen/fontsize" />

希望这可能对某人有所帮助。 一切顺利。