Android CalendarView当前月份和年份未显示

时间:2015-01-08 12:35:37

标签: android calendarview

我在对话框中添加了日历视图。单击按钮时会出现对话框。我遇到的问题是,当出现对话框时,日历视图不会显示当前月份和年份。但是当我更改月份时,会显示月份和年份(当月也是如此)。我想在初始对话节目中显示这个月和年份。请帮帮我。提前谢谢。

我没有足够的声誉来发布截屏。对不起。

这是代码:

date.setOnClickListener(new OnClickListener() {         
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService
                      (Context.LAYOUT_INFLATER_SERVICE);
            RelativeLayout ll= (RelativeLayout)inflater.inflate(R.layout.calendar_dialog, null, false);
            final CalendarView cv = (CalendarView) ll.getChildAt(0);
            cv.setOnDateChangeListener(new OnDateChangeListener() {
                @Override
                public void onSelectedDayChange(CalendarView view, int year, int month,
                        int dayOfMonth) {
                    // TODO Auto-generated method stub
                    String date = dayOfMonth+"/"+(month+1)+"/"+year;
                    filterNews(date);
                }
            });

            AlertDialog.Builder builder = new AlertDialog.Builder(FirstView.this, AlertDialog.THEME_HOLO_DARK);
            builder.setTitle("News Calendar")
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        // Do nothing.
                    }
                }
            );
            AlertDialog alertDialog = builder.create();
            alertDialog.show();         
        }
    });

日期是一个按钮。

3 个答案:

答案 0 :(得分:0)

final Calendar c = Calendar.getInstance();
        maxyear = c.get(Calendar.YEAR);
        maxmonth = c.get(Calendar.MONTH);
        maxday = c.get(Calendar.DAY_OF_MONTH);


DatePickerDialog _date =   new DatePickerDialog(this, datePickerListener, maxyear,maxmonth,
                    maxday)

答案 1 :(得分:0)

只是您可以应用于对话框的工作CalendarView的简短示例:

CalendarView calendar;

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);
   calendar = (CalendarView) findViewById(R.id.calendar);
   calendar.setOnDateChangeListener(new OnDateChangeListener() {

      @Override
      public void onSelectedDayChange(CalendarView view,
      int year, int month, int dayOfMonth) {
      Toast.makeText(getApplicationContext(), dayOfMonth +"/"+month+"/"+ year,Toast.LENGTH_LONG).show();}});
}

答案 2 :(得分:0)

   <CalendarView
    android:id="@+id/calendar"
    android:layout_margin="10dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

在Xml文件中使用 在清单中你应该改变

android:minSdkVersion="11"

Callendar视图需要api 11:)