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

时间:2012-04-26 19:10:11

标签: android

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

我也使用了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);

6 个答案:

答案 0 :(得分:3)

在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
 });

答案 1 :(得分:2)

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();
    }
});

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

答案 2 :(得分:0)

Calendar c = Calendar.getInstance();

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

答案 3 :(得分:0)

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();
    }
});

答案 4 :(得分:0)

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);

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

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);

答案 5 :(得分:0)

Kotlin 获取所选日期的简单 CalenderView 代码:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <CalendarView
        android:id="@+id/calendar_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Toast Selected Date" />

</LinearLayout>

MainActivity.kt

import android.os.Bundle
import android.text.format.DateFormat
import android.util.Log
import android.widget.CalendarView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.testandcheck.databinding.ActivityMainBinding
import java.util.*

class MainActivity : AppCompatActivity() {

private lateinit var binding: ActivityMainBinding   // Binding object for ViewBinding.

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivityMainBinding.inflate(layoutInflater)
    setContentView(binding.root)

    // Set date change listener on calenderView.
    // Callback notified when user select a date from CalenderView on UI.
    binding.calendarView.setOnDateChangeListener { calView: CalendarView, year: Int, month: Int, dayOfMonth: Int ->

        // Create calender object with which will have system date time.
        val calender: Calendar = Calendar.getInstance()

        // Set attributes in calender object as per selected date.
        calender.set(year, month, dayOfMonth)

        // Now set calenderView with this calender object to highlight selected date on UI.
        calView.setDate(calender.timeInMillis, true, true)
        Log.d("SelectedDate", "$dayOfMonth/${month + 1}/$year")
    }

    // Example button to show get selected date from CalenderView and show in Toast.
    binding.button.setOnClickListener {

        // Fetch long milliseconds from calenderView.
        val dateMillis: Long = binding.calendarView.date

        // Create Date object from milliseconds.
        val date: Date = Date(dateMillis)

        // Get Date values and created formatted string date to show in Toast.
        val selectedDayOfWeek = DateFormat.format("EEEE", date) as String // Monday
        val selectedDay = DateFormat.format("dd", date) as String // 05
        val selectedMonthString = DateFormat.format("MMM", date) as String // Jul
        val selectedMonthNumber = DateFormat.format("MM", date) as String // 6 --> Month Code as Jan = 0 till Dec = 11.
        val selectedYear = DateFormat.format("yyyy", date) as String // 2021

        val strFormattedSelectedDate = "$selectedDay-${selectedMonthNumber + 1}-$selectedYear ($selectedDayOfWeek)"
        Toast.makeText(applicationContext, "Selected Date = $strFormattedSelectedDate", Toast.LENGTH_SHORT).show()
    }
}

}
<块引用>

注意:一旦您选择了 CalenderView 上的任何日期,它只会突出显示该日期而不是选择。由于这个原因,当您调用 calendarView.date (kotlin) 或 calenderView.getDate() (Java) 时,您将始终获得系统的当前日期时间。所以你必须实现setOnDateChangeListener。收到回调后,您必须以编程方式设置新的 Date 对象,如图所示。

希望它有帮助......快乐编码...... :)