6

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

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

6 回答 6

5

在 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
 });
于 2012-04-26T19:32:40.320 回答
3
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();
    }
});

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

于 2012-09-01T03:43:29.187 回答
0
Calendar c = Calendar.getInstance();

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

于 2012-04-26T19:29:09.380 回答
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();
    }
});
于 2013-05-10T13:23:27.350 回答
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对象,如图所示。

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

于 2021-07-05T08:09:18.260 回答
-1
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);
于 2014-02-24T05:37:12.223 回答