Android:日历弹出窗口中的日期选择器

在我的应用程序中,用户应该通过单击编辑文本来选择日期,因为目前我正在使用日期选择器对话框并且它的工作正常,但它就像打开另一个对话框一样.所以我想把它显示为弹出菜单,结帐图片.请帮我实现这个目标!

解决方法:

您只需创建一个New Popup窗口并将日历视图膨胀到该窗口:按照以下步骤操作:

edit_text.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                showPopup(MainActivity.this);
            }
        });

现在showPopup()方法:

// The method that displays the popup.

private void showPopup(Activity context) {

               // Inflate the popup_layout.xml
               LayoutInflater layoutInflater = (LayoutInflater)getBaseContext() 
                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
               layout = layoutInflater.inflate(R.layout.main3, null,false);
               // Creating the PopupWindow
               final PopupWindow popupWindow = new PopupWindow(
                          layout,400,400);

               popupWindow.setContentView(layout);
               popupWindow.setHeight(500);
               popupWindow.setOutsideTouchable(false);
               // Clear the default translucent background
               popupWindow.setBackgroundDrawable(new BitmapDrawable()); 

                CalendarView cv = (CalendarView) layout.findViewById(R.id.calendarView);
                cv.setBackgroundColor(Color.BLUE);

                cv.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {

                @Override
                public void onSelectedDayChange(CalendarView view, int year, int month,
                        int dayOfMonth) {
                    // TODO Auto-generated method stub
                    popupWindow.dismiss();
                    Log.d("date selected", "date selected " + year + " " + month + " " + dayOfMonth);

                    }
                });
              popupWindow.showAtLocation(layout, Gravity.TOP,5,170);
            }

现在,main3.xml文件:

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/layout"
  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:clickable="true"
    android:showWeekNumber="false" />

 </LinearLayout>

然后在快照下面看看你会自己定制这个弹出窗口.

上一篇:php – 通过日期选择器日历从选定日期获取数据


下一篇:javascript – 如何从textfield中的datepicker获取值