如何在Android中的日历视图中获取所选日期?

时间:2013-04-16 07:38:05

标签: android mobile

Calendar date = Calendar.getInstance();     
SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd");

curDate = sdf.format(date.getTime());

我在onCreate方法中完成了上面的代码。但它只能返回当前日期。我想要的是在日历中选择日期

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

请帮帮我

5 个答案:

答案 0 :(得分:9)

我使用CalendarView代替日历,现在正在使用。

calendarView.setOnDateChangeListener(new OnDateChangeListener() {

                @Override
                public void onSelectedDayChange(CalendarView view, int year, int month,
                        int dayOfMonth) {
                    int d = dayOfMonth;
                    curDate =String.valueOf(d);
                }
            });

答案 1 :(得分:0)

 @IBAction func takeThermalPicture(_ sender: Any) {

        FLIROneSDKStreamManager.sharedInstance().addDelegate(nil)
        cameraBussy = true
        icnCancelPicture.isHidden = false
        icnUploadPicture.isHidden = false
        iconTakePicture.isHidden = true

    }

答案 2 :(得分:0)

您可以像这样使用日历视图获取选定日期...

CalendarView calendarView=view.findViewById(R.id.calendarView);

    calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {

        @Override
        public void onSelectedDayChange(CalendarView view, int year, int month,
                                        int dayOfMonth) {
          String  curDate = String.valueOf(dayOfMonth);
          String  Year = String.valueOf(year);
          String  Month = String.valueOf(month);

            Log.e("date",Year+"/"+Month+"/"+curDate);
        }
    });

答案 3 :(得分: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()
    }
}

}

说明:

  1. 设置侦听器以在 CalenderView 中设置所选日期(同时突出显示新选择的日期并取消突出显示旧日期)。
  2. 然后只调用 calenderView.date 来获取选定的日期。

答案 4 :(得分:-2)

您是否尝试过Calendar.get(Calendar.DATE)

检查链接GetDate() Calendar