Cal droid Calender在android活动中用作日期选择器

Cal droid Calender在android活动中用作日期选择器,android,caldroid,Android,Caldroid,有人能告诉我如何在android活动中创建一个日期选择器吗 下面是一个示例应用程序: 以下是活动的代码: @SuppressLint("SimpleDateFormat") public class CaldroidSampleActivity extends AppCompatActivity { private boolean undo = false; private CaldroidFragment caldroidFragment; private Caldro

有人能告诉我如何在android活动中创建一个日期选择器吗

下面是一个示例应用程序:

以下是活动的代码:

@SuppressLint("SimpleDateFormat")
public class CaldroidSampleActivity extends AppCompatActivity {
    private boolean undo = false;
    private CaldroidFragment caldroidFragment;
    private CaldroidFragment dialogCaldroidFragment;

    private void setCustomResourceForDates() {
        Calendar cal = Calendar.getInstance();

        // Min date is last 7 days
        cal.add(Calendar.DATE, -7);
        Date blueDate = cal.getTime();

        // Max date is next 7 days
        cal = Calendar.getInstance();
        cal.add(Calendar.DATE, 7);
        Date greenDate = cal.getTime();

        if (caldroidFragment != null) {
            ColorDrawable blue = new ColorDrawable(getResources().getColor(R.color.blue));
            ColorDrawable green = new ColorDrawable(Color.GREEN);
            caldroidFragment.setBackgroundDrawableForDate(blue, blueDate);
            caldroidFragment.setBackgroundDrawableForDate(green, greenDate);
            caldroidFragment.setTextColorForDate(R.color.white, blueDate);
            caldroidFragment.setTextColorForDate(R.color.white, greenDate);
        }
    }

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

        final SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy");

        // Setup caldroid fragment
        // **** If you want normal CaldroidFragment, use below line ****
        caldroidFragment = new CaldroidFragment();

        // //////////////////////////////////////////////////////////////////////
        // **** This is to show customized fragment. If you want customized
        // version, uncomment below line ****
//       caldroidFragment = new CaldroidSampleCustomFragment();

        // Setup arguments

        // If Activity is created after rotation
        if (savedInstanceState != null) {
            caldroidFragment.restoreStatesFromKey(savedInstanceState,
                    "CALDROID_SAVED_STATE");
        }
        // If activity is created from fresh
        else {
            Bundle args = new Bundle();
            Calendar cal = Calendar.getInstance();
            args.putInt(CaldroidFragment.MONTH, cal.get(Calendar.MONTH) + 1);
            args.putInt(CaldroidFragment.YEAR, cal.get(Calendar.YEAR));
            args.putBoolean(CaldroidFragment.ENABLE_SWIPE, true);
            args.putBoolean(CaldroidFragment.SIX_WEEKS_IN_CALENDAR, true);

            // Uncomment this to customize startDayOfWeek
            // args.putInt(CaldroidFragment.START_DAY_OF_WEEK,
            // CaldroidFragment.TUESDAY); // Tuesday

            // Uncomment this line to use Caldroid in compact mode
            // args.putBoolean(CaldroidFragment.SQUARE_TEXT_VIEW_CELL, false);

            // Uncomment this line to use dark theme
//            args.putInt(CaldroidFragment.THEME_RESOURCE, com.caldroid.R.style.CaldroidDefaultDark);

            caldroidFragment.setArguments(args);
        }

        setCustomResourceForDates();

        // Attach to the activity
        FragmentTransaction t = getSupportFragmentManager().beginTransaction();
        t.replace(R.id.calendar1, caldroidFragment);
        t.commit();

        // Setup listener
        final CaldroidListener listener = new CaldroidListener() {

            @Override
            public void onSelectDate(Date date, View view) {
                Toast.makeText(getApplicationContext(), formatter.format(date),
                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onChangeMonth(int month, int year) {
                String text = "month: " + month + " year: " + year;
                Toast.makeText(getApplicationContext(), text,
                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onLongClickDate(Date date, View view) {
                Toast.makeText(getApplicationContext(),
                        "Long click " + formatter.format(date),
                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCaldroidViewCreated() {
                if (caldroidFragment.getLeftArrowButton() != null) {
                    Toast.makeText(getApplicationContext(),
                            "Caldroid view is created", Toast.LENGTH_SHORT)
                            .show();
                }
            }

        };

        // Setup Caldroid
        caldroidFragment.setCaldroidListener(listener);

        final TextView textView = (TextView) findViewById(R.id.textview);

        final Button customizeButton = (Button) findViewById(R.id.customize_button);

        // Customize the calendar
        customizeButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (undo) {
                    customizeButton.setText(getString(R.string.customize));
                    textView.setText("");

                    // Reset calendar
                    caldroidFragment.clearDisableDates();
                    caldroidFragment.clearSelectedDates();
                    caldroidFragment.setMinDate(null);
                    caldroidFragment.setMaxDate(null);
                    caldroidFragment.setShowNavigationArrows(true);
                    caldroidFragment.setEnableSwipe(true);
                    caldroidFragment.refreshView();
                    undo = false;
                    return;
                }

                // Else
                undo = true;
                customizeButton.setText(getString(R.string.undo));
                Calendar cal = Calendar.getInstance();

                // Min date is last 7 days
                cal.add(Calendar.DATE, -7);
                Date minDate = cal.getTime();

                // Max date is next 7 days
                cal = Calendar.getInstance();
                cal.add(Calendar.DATE, 14);
                Date maxDate = cal.getTime();

                // Set selected dates
                // From Date
                cal = Calendar.getInstance();
                cal.add(Calendar.DATE, 2);
                Date fromDate = cal.getTime();

                // To Date
                cal = Calendar.getInstance();
                cal.add(Calendar.DATE, 3);
                Date toDate = cal.getTime();

                // Set disabled dates
                ArrayList<Date> disabledDates = new ArrayList<Date>();
                for (int i = 5; i < 8; i++) {
                    cal = Calendar.getInstance();
                    cal.add(Calendar.DATE, i);
                    disabledDates.add(cal.getTime());
                }

                // Customize
                caldroidFragment.setMinDate(minDate);
                caldroidFragment.setMaxDate(maxDate);
                caldroidFragment.setDisableDates(disabledDates);
                caldroidFragment.setSelectedDates(fromDate, toDate);
                caldroidFragment.setShowNavigationArrows(false);
                caldroidFragment.setEnableSwipe(false);

                caldroidFragment.refreshView();

                // Move to date
                // cal = Calendar.getInstance();
                // cal.add(Calendar.MONTH, 12);
                // caldroidFragment.moveToDate(cal.getTime());

                String text = "Today: " + formatter.format(new Date()) + "\n";
                text += "Min Date: " + formatter.format(minDate) + "\n";
                text += "Max Date: " + formatter.format(maxDate) + "\n";
                text += "Select From Date: " + formatter.format(fromDate)
                        + "\n";
                text += "Select To Date: " + formatter.format(toDate) + "\n";
                for (Date date : disabledDates) {
                    text += "Disabled Date: " + formatter.format(date) + "\n";
                }

                textView.setText(text);
            }
        });

        Button showDialogButton = (Button) findViewById(R.id.show_dialog_button);

        final Bundle state = savedInstanceState;
        showDialogButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // Setup caldroid to use as dialog
                dialogCaldroidFragment = new CaldroidFragment();
                dialogCaldroidFragment.setCaldroidListener(listener);

                // If activity is recovered from rotation
                final String dialogTag = "CALDROID_DIALOG_FRAGMENT";
                if (state != null) {
                    dialogCaldroidFragment.restoreDialogStatesFromKey(
                            getSupportFragmentManager(), state,
                            "DIALOG_CALDROID_SAVED_STATE", dialogTag);
                    Bundle args = dialogCaldroidFragment.getArguments();
                    if (args == null) {
                        args = new Bundle();
                        dialogCaldroidFragment.setArguments(args);
                    }
                } else {
                    // Setup arguments
                    Bundle bundle = new Bundle();
                    // Setup dialogTitle
                    dialogCaldroidFragment.setArguments(bundle);
                }

                dialogCaldroidFragment.show(getSupportFragmentManager(),
                        dialogTag);
            }
        });
    }

    /**
     * Save current states of the Caldroid here
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // TODO Auto-generated method stub
        super.onSaveInstanceState(outState);

        if (caldroidFragment != null) {
            caldroidFragment.saveStatesToKey(outState, "CALDROID_SAVED_STATE");
        }

        if (dialogCaldroidFragment != null) {
            dialogCaldroidFragment.saveStatesToKey(outState,
                    "DIALOG_CALDROID_SAVED_STATE");
        }
    }

}
@SuppressLint(“SimpleDataFormat”)
公共类CaldroidSampleActivity扩展了AppCompative活动{
私有布尔撤消=false;
私人CaldroidFragment CaldroidFragment;
专用CaldroidFragment对话框CaldroidFragment;
私有void setCustomResourceForDates(){
Calendar cal=Calendar.getInstance();
//最小日期为最后7天
计算添加(日历日期,-7);
Date blueDate=cal.getTime();
//最长日期为未来7天
cal=Calendar.getInstance();
计算添加(日历日期,7);
Date greenDate=cal.getTime();
如果(caldroidFragment!=null){
ColorDrawable blue=新的ColorDrawable(getResources().getColor(R.color.blue));
可着色绿色=新的可着色(Color.green);
caldroidFragment.setBackgroundDrawableForDate(蓝色,蓝色日期);
caldroidFragment.setBackgroundDrawableForDate(绿色,绿色日期);
caldroidFragment.setTextColorForDate(R.color.white,blueDate);
caldroidFragment.setTextColorForDate(R.color.white,greenDate);
}
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
最终SimpleDataFormat格式化程序=新SimpleDataFormat(“dd MMM yyyy”);
//设置caldroid片段
//****如果您想要普通CaldroidFragment,请使用下面的行****
caldroidFragment=新的caldroidFragment();
// //////////////////////////////////////////////////////////////////////
//****这是为了显示自定义片段。如果要自定义
//版本,在第行下方取消注释****
//caldroidFragment=新的CaldroidSampleCustomFragment();
//设置参数
//如果活动是在旋转后创建的
如果(savedInstanceState!=null){
caldroidFragment.restoreStatesFromKey(savedInstanceState,
“卡尔德罗德救了州”);
}
//如果活动是从新创建的
否则{
Bundle args=新Bundle();
Calendar cal=Calendar.getInstance();
args.putInt(CaldroidFragment.MONTH,cal.get(Calendar.MONTH)+1);
args.putInt(CaldroidFragment.YEAR,cal.get(Calendar.YEAR));
args.putBoolean(CaldroidFragment.ENABLE_SWIPE,true);
args.putBoolean(日历中的CaldroidFragment.SIX_WEEKS_,true);
//取消对此的注释以自定义startDayOfWeek
//参数putInt(CaldroidFragment.START_DAY_OF u WEEK,
//CaldroidFragment.周二);//周二
//取消对此行的注释以在压缩模式下使用Caldroid
//args.putBoolean(CaldroidFragment.SQUARE\u TEXT\u VIEW\u CELL,false);
//取消对此行的注释以使用深色主题
//args.putInt(CaldroidFragment.THEME_资源,com.caldroid.R.style.CaldroidDefaultDark);
caldroidFragment.setArguments(args);
}
setCustomResourceForDates();
//参加活动
FragmentTransaction t=getSupportFragmentManager().beginTransaction();
t、 更换(R.id.calendar1,caldroidFragment);
t、 提交();
//设置侦听器
最终CaldroidListener侦听器=新的CaldroidListener(){
@凌驾
在选择日期(日期、视图)上的公共无效{
Toast.makeText(getApplicationContext(),formatter.format(日期),
吐司。长度(短)。show();
}
@凌驾
变更月公共作废(整数月、整数年){
String text=“月:+月+”年:+年;
Toast.makeText(getApplicationContext(),text,
吐司。长度(短)。show();
}
@凌驾
仅限长点击日期的公共作废(日期,查看){
Toast.makeText(getApplicationContext(),
“长按”+格式化程序。格式化(日期),
吐司。长度(短)。show();
}
@凌驾
公共void onCaldroidViewCreated(){
如果(caldroidFragment.getLeftArrowButton()!=null){
Toast.makeText(getApplicationContext(),
“Caldroid视图已创建”,Toast.LENGTH_SHORT)
.show();
}
}
};
//设置Caldroid
caldroidFragment.setCaldroidListener(侦听器);
最终文本视图文本视图=(文本视图)findViewById(R.id.TextView);
最终按钮customizeButton=(按钮)findViewById(R.id.customize_按钮);
//自定义日历
setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
如果(撤消){
customizeButton.setText(getString(R.string.customize));
textView.setText(“”);
//重置日历
caldroidFragment.clearDisableDates();
caldroidFragment.clearSelectedDates();
caldroidFragment.setMinDate(null);
caldroidFragment.setMaxDate(null);
caldroidFragment.SetShowNavigationRows(true);
caldroidFragment.setEnableSwipe(true);
caldroidFragment.refreshView();
撤销=假;
返回;
}
//否则
撤销=真;
customizeButton.setText(getString(R.string.undo));
Calendar cal=Calendar.getInstance();
//最小日期为最后7天
cal.add(Calendar.DA