api级别高于11的日期选择器的Android日历视图

时间:2013-04-01 09:10:46

标签: android dialog calendar

我想编写一个带按钮的应用程序,当我点击按钮时,我想在对话框中的Datepicker对话框中显示日历。

我尝试使用new DatePickerDialog.OnDateSetListener()构造函数来查找日历。是不是可以这样做?

在api level 11之后,它支持日历视图。

2 个答案:

答案 0 :(得分:1)

您好请尝试下面的代码希望它能帮助您:

  <Button
            android:id="@+id/birthday"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

现在将此按钮调入您的活动

    @Override
    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_register);

         editBirthday = (Button)findViewById(R.id.birthday);


         /*
         * Change Birth day on click of edit box
         */
        editBirthday.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub

                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(editBirthday.getWindowToken(), 0);
                showDialog(DATE_DIALOG_ID);
                return true;
            }
        });

    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
            return new DatePickerDialog(this, mDateSetListener, year, month,
                    day);
        }
        return null;
    }

    // updates the date we display in the TextView
    private void updateDisplay() {
        /*
         * Hide virtual keyboard
         */
        editBirthday.setText(new StringBuilder()
                // Month is 0 based so add 1
                .append(year).append("-").append(month + 1).append("-")
                .append(day).append(""));
    }

    private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

        public void onDateSet(DatePicker view, int myear, int monthOfYear,
                int dayOfMonth) {
            year = myear;
            month = monthOfYear;
            day = dayOfMonth;
            updateDisplay();
        }
    };

答案 1 :(得分:1)

//此代码在对话框中显示日历,希望它可以帮助您

LayoutInflater inflater =(LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    LinearLayout ll = (LinearLayout)inflater.inflate(R.layout.calendar, null,  false);
    CalendarView cv = (CalendarView) ll.findViewById(R.id.calendarView);
    cv.setBackgroundColor(Color.BLACK);


    cv.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {

    @Override
    public void onSelectedDayChange(CalendarView view, int year, int month,
            int dayOfMonth) {
        // TODO Auto-generated method stub

        Log.d("date selected", "date selected " + year + " " + month + " " + dayOfMonth);
        int rows = showMapFragment(year, month, dayOfMonth);

        if (rows == 0){
            Toast.makeText(getApplicationContext(), year + "/" + (month + 1) +"/" + dayOfMonth + " No route to display", Toast.LENGTH_SHORT).show();
        }

        }
    });

    ll.findViewById(R.id.calendarView).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.d("calendar view", "calendar view clicked");
        }
    });

    Dialog calendarDialog = new Dialog(Main.this);
    calendarDialog.setContentView(ll);




    calendarDialog.setTitle("Pick a date to view your performance and route");
    calendarDialog.show();    

//以下是日历布局xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<CalendarView
android:id="@+id/calendarView"
android:layout_width="match_parent"
android:layout_height= "400dp"
android:showWeekNumber="false"
android:clickable="true"
android:weekDayTextAppearance="@style/calendarStyle"
android:dateTextAppearance="@style/calendarStyle" 
/>

</LinearLayout>