6

我在互联网上进行了很多搜索,但无法成功找到 CalendarView 单击今天日期的正确解决方案。

在此处输入图像描述

我需要将 CalendarView 用于事件应用程序。 setOnDateChangeListener适用于其他天的点击。

代码:

CalendarView calendarView=(CalendarView) findViewById(R.id.calendarView1);
    calendarView.setOnDateChangeListener(new OnDateChangeListener() {

        @Override
        public void onSelectedDayChange(CalendarView view, int year, int month,
                int dayOfMonth) {
            Intent k = new Intent(GlavnaAktivnost.this, DatumDetalji.class);
            k.putExtra("godina", year);
            k.putExtra("mesec", month);
            k.putExtra("dan", dayOfMonth);
            startActivity(k);

        }
    });

我不太明白我需要做什么,最好的解决方案是什么?我能够在堆栈上找到的答案是扩展 CalendarView 或绑定单击 UI,但我找不到解决方法。你能给我举个例子吗?

肿瘤坏死因子。

4

8 回答 8

3

setOnDateChangeListener不会开火,但OnGlobalLayoutListener会。因此,如果您尝试:

calendarView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){
    @Override
    public void onGlobalLayout(){
    }
});
于 2015-08-06T15:01:51.883 回答
1

对CalendarView.java的审查表明,突出显示的行被消耗触摸事件的内部私有类覆盖,因此 OnClickListener() 和 OnTouchListener() 都不起作用。

第一个(更难的)选项是制作您自己的 CalendarView.java 副本(称为 MyCustomCalendarView.java)并将其包含到您的项目中。在 MyCustomCalendarView.java 中,添加代码来注册 OnTouchListener,如下所示:

private View.OnTouchListener touchListener;
@Override
public void setOnTouchListener( View.OnTouchListener otl) {
    touchListener = otl;
}

然后在私有类 WeeksAdapter 的 OnTouch() 方法中,添加这一行:

@Override
 public boolean onTouch(View v, MotionEvent event) {
     if (mListView.isEnabled() && mGestureDetector.onTouchEvent(event)) {
         touchListener.onTouch(v, event);
     ...

然后 MyCustomCalendarView 可以按如下方式使用:

MyCustomCalendarView thisCalendar = (MyCustomCalendarView) rootView.findViewById(R.id.mycustom_calendar_view_layout_id);
thisCalendar.setOnTouchListener(myCustomListener);

另一种(更简单)的替代方法是在 calendarView 布局中添加一个按钮,并使用按钮的 onClickListener(可能还有一个标志变量)来检测是否选择了任何日期。在这种情况下,从设备的角度来看,选择当前日期与“未选择日期”相同,因此您可以使用该事实来写入今天的日期或其他自定义操作。

您可以使用datetimepicker-library的日期选择器布局来帮助您入门:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    android:gravity="center" 
    android:orientation="vertical" 
    android:background="@color/date_picker_view_animator" 
    android:layout_width="@dimen/date_picker_component_width" 
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout 
        android:orientation="vertical" 
        android:layout_width="wrap_content" 
        android:layout_height="@dimen/selected_calendar_layout_height">
        <include layout="@layout/date_picker_selected_date" />
    </LinearLayout>

    <!-- this is the calendarView, add your own version here as you see fit -->
    <include layout="@layout/date_picker_view_animator" />

    <!-- this is the button to be added to this layout -->
    <View 
        android:background="@color/line_background" 
        android:layout_width="@dimen/date_picker_component_width" 
        android:layout_height="1.0dip" />
        <include layout="@layout/date_picker_done_button" />
</LinearLayout>
于 2014-08-10T17:07:42.390 回答
1

尝试使用下一种方式:

Date nowDate = new Date();

int mYear = nowDate.getYear();
int mMonth = nowDate.getMonth();
int mDay = nowDate.getDay();

CalendarView calendarView = (CalendarView) findViewById(R.id.calendarView1);
calendarView.setOnDateChangeListener(new OnDateChangeListener() {

    @Override
    public void onSelectedDayChange(CalendarView view, int year, int month,
        int dayOfMonth) {

        mYear = year;
        mMonth = month;
        mDay = dayOfMonth;

        openGlavnaAktivnost();
    }
});


private void openGlavnaAktivnost(){

    Intent k = new Intent(GlavnaAktivnost.this, DatumDetalji.class);
    k.putExtra("godina", mYear);
    k.putExtra("mesec", mMonth);
    k.putExtra("dan", mDay);
    startActivity(k);
}
于 2019-06-27T07:56:46.803 回答
1

试试下面的方法:

//First set current day,month,year :

Calendar c = Calendar.getInstance();
int prevDay = c.get(Calendar.DAY_OF_MONTH);
int prevMonth = c.get(Calendar.MONTH);
int prevYear = c.get(Calendar.YEAR);
Date d = new Date(prevYear, prevMonth, prevDay);
c.setTime(d);

然后使用 dateChangeListener :

CalendarView calendar = (CalendarView) findViewById(R.id.calendarViewCN);
calendar.setOnDateChangeListener(new OnDateChangeListener() {
@Override
public void onSelectedDayChange(CalendarView view, int year,int month, int dayOfMonth) {
<b>boolean changed = changeUpdate(year, month, dayOfMonth);</b>
if (changed) {
Toast.makeText(getApplicationContext(),
"New Y: " + year + " M: " + month + " D: "+dayOfMonth, Toast.LENGTH_LONG).show();
}
}
});

现在声明 changeUpdate() :

private boolean changeUpdate(int curYear, int curMonth, int curDay) {
boolean changed = false;
if (curDay != prevDay || curMonth!=prevMonth || curYear!=prevYear) {
changed = true;
}
于 2015-07-10T17:30:53.963 回答
1

不是直接的解决方案,但我最终所做的是忽略OnDateChangeListener并添加一个 SELECT 按钮。也不是我想要的解决方案,但它有效......

https://stackoverflow.com/a/33635580/1121497

于 2015-11-10T17:09:14.713 回答
0

尝试这个:

CalendarView calendarView=(CalendarView) findViewById(R.id.calendarView1);
calendarView.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(getDate());
        Intent k = new Intent(GlavnaAktivnost.this, DatumDetalji.class);
        k.putExtra("godina", calendar.get(Calendar.YEAR);
        k.putExtra("mesec", calendar.get(Calendar.MONTH);
        k.putExtra("dan", calendar.get(Calendar.DAY_OF_MONTH);
        startActivity(k);

    }
});
于 2013-10-24T14:05:44.913 回答
0

我使用了以下课程。线程睡眠有点破解,但可以增加睡眠时间。

public class CustomCalendarView extends CalendarView {

    private final String LOG_HEADER = "CAL:VW";
    private Date previousSelectedDate = new Date();
    private OnDateChangeListener listener;
    private CheckSameSelectedDateAsyncTask task = new CheckSameSelectedDateAsyncTask();

    public CustomCalendarView(Context context) {
        super(context);
    }

    public CustomCalendarView(Context context, AttributeSet attribute) {
        super(context, attribute);
    }

    public CustomCalendarView(Context context, AttributeSet attribute, int defStyle) {
        super(context, attribute, defStyle);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if ((task.getStatus() == AsyncTask.Status.PENDING) || (task.getStatus() == AsyncTask.Status.FINISHED)) {
            task = new CheckSameSelectedDateAsyncTask();
            task.execute();
        }
        return false;
    }

    private void checkSameSelectedDate() {
        Date selectedDate = new Date(super.getDate());
        if (selectedDate.getDay() == previousSelectedDate.getDay() &&
                selectedDate.getMonth() == previousSelectedDate.getMonth() &&
                selectedDate.getYear() == previousSelectedDate.getYear()) {
            if (listener != null) {
                this.listener.onSameSelectedDayChange(this, selectedDate.getYear(), selectedDate.getMonth(), selectedDate.getDay());
            }
        }
        this.previousSelectedDate = selectedDate;
    }

    public void setSameSelectedDayChangeListener(OnDateChangeListener listener) {
        this.listener = listener;
    }

    public interface OnDateChangeListener extends CalendarView.OnDateChangeListener {
        void onSameSelectedDayChange(CalendarView view, int year, int month, int day);
    }

    private class CheckSameSelectedDateAsyncTask extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... params) {
            try {
                //Note: Breaking point between 75 - 100
                Thread.sleep(300);
            } catch (InterruptedException ex) {
                Thread.currentThread().interrupt();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            checkSameSelectedDate();
        }
    }
}
于 2017-07-20T14:48:31.480 回答
0

为我工作/字符串:

    Intent intent = new Intent(MainActivity.this, DayActivity.class);
            intent.putExtra("year", String.valueOf(year) );
            intent.putExtra("mon", String.valueOf(month));
            intent.putExtra("day", String.valueOf(dayM));
            startActivity(intent);

或需要将 int 转换为 String 以在 TextView 中设置文本

于 2016-02-22T18:45:11.650 回答