Android 如何从 CalendarView 中提取日期并显示所选日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10339808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:20:51  来源:igfitidea点击:

How can I extract date from CalendarView and display selected date?

android

提问by GuyWhoReadsStockoverflow

Currently I am using code below and I can get current date to be dispalyed in Text field. However I have no idea how to display selected date.

目前我正在使用下面的代码,我可以在文本字段中显示当前日期。但是我不知道如何显示选定的日期。

I also used DatePicker and it displays selected date fine, however I would prefer CalendarView. Thanks

我还使用了 DatePicker,它可以很好地显示所选日期,但是我更喜欢 CalendarView。谢谢

Calendar c = Calendar.getInstance();
        SimpleDateFormat ss = new SimpleDateFormat("dd-MM-yyyy");
        Date date = new Date();
        String currentdate= ss.format(date);
        TextView editText = (TextView) this.findViewById(R.id.textfield1);
        editText.setText(currentdate);

采纳答案by Snicolas

in onCreate :

在 onCreate 中:

 CalendarView v = new CalendarView( this );
 v.setOnDateChangeListener( new CalendarView.OnDateChangeListener() {
    public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
       this.calendar = new GregorianCalendar( year, month, dayOfMonth );
    }//met
 });

回答by Ravi

CalendarView view = new CalendarView(this);
setContentView(view);

view.setOnDateChangeListener(new OnDateChangeListener() {

    @Override
    public void onSelectedDayChange(CalendarView arg0, int year, int month,
        int date) {
        Toast.makeText(getApplicationContext(),date+ "/"+month+"/"+year,4000).show();
    }
});

This will display the user selected date.

这将显示用户选择的日期。

回答by sara

CalendarView view = new CalendarView(this);
setContentView(view);

view.setOnDateChangeListener(new OnDateChangeListener() {

    @Override
    public void onSelectedDayChange(CalendarView arg0, int year, int month,
        int date) {
        Toast.makeText(getApplicationContext(),date+ "/"+month+"/"+year,4000).show();
    }
});

回答by Mehdi

Calendar c = Calendar.getInstance();
    SimpleDateFormat ss = new SimpleDateFormat("dd-MM-yyyy");
    Date date = new Date();
    String currentdate= ss.format(date);
    TextView editText = (TextView) this.findViewById(R.id.textfield1);
    editText.setText(currentdate);

above one is ur code try below code

上面是你的代码试试下面的代码

CalendarView cv = (CalendarView)findViewById(R.id.cv1);
Calendar c = Calendar.getInstance();
    SimpleDateFormat ss = new SimpleDateFormat("dd-MM-yyyy");
    Date date = new Date(cv);
    String currentdate= ss.format(date);
    TextView editText = (TextView) this.findViewById(R.id.textfield1);
    editText.setText(currentdate);

回答by Sam

Calendar c = Calendar.getInstance();

Calendaris different from CalendarView. CalendarViewis a descendant of Viewso it has many event handlers, like an onClickListener. Perhaps this is what you are trying to find?

CalendarCalendarView不同。CalendarView是 的后代,View因此它有许多事件处理程序,例如onClickListener。也许这就是你想要找到的?