Android onSelectedDayChange 对不同 api 级别的工作方式不同

Android onSelectedDayChange works differently for different api level

我使用 CalendarView 保存事件,但它不适用于某些设备! 它在图形上有所不同(有些设备需要滚动几个月,有些设备有指向屏幕顶部的箭头来更改月份)。

但主要问题是带有箭头更改月份的设备,方法onSelectedDayChange不起作用(api 23级)!

这是我使用的代码

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CalendarView;


public class CalendarioActivity extends ActionBarActivity {

    CalendarView calendar;
    Long date;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calendario);


        calendar = (CalendarView)findViewById(R.id.calendarView);
        date = calendar.getDate();

        calendar.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {

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


                /*Con questo if blocco onSelectedDayChange quando viene scrollato
                il calendario per cambiare mese*/
                if(calendar.getDate() != date) {
                    date = calendar.getDate();
                   /* Quando seleziono un giorno apro l'activity per scriverci i compiti sopra*/
                    /*Passo tramite intent giorno,mese,anno*/
                    Intent myIntent = new Intent(CalendarioActivity.this, ActivityEvento.class);
                    myIntent.putExtra("day",dayOfMonth).putExtra("month", month+1).putExtra("year", year);
                    startActivity(myIntent);
                }
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_calendario, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        switch (item.getItemId()) {

            case R.id.action_help:
                Intent aiuto_intent = new Intent(this, AiutoActivity.class);
                aiuto_intent.putExtra("chiamante", "calendario");
                startActivity(aiuto_intent);
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

这是带有日历视图的xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.ddz.diarioscolastico.CalendarioActivity">


    <CalendarView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/calendarView"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:unfocusedMonthDateColor="@color/grigio_chiaro"
        android:dateTextAppearance="@android:style/TextAppearance.Large"
        android:showWeekNumber="false"/>

</RelativeLayout>

问题是IF

if(calendar.getDate() != date) {
   date = calendar.getDate();

   Intent myIntent = new Intent(CalendarioActivity.this,ActivityEvento.class); 
   myIntent.putExtra("day",dayOfMonth).putExtra("month", month+1).putExtra("year", year);
   startActivity(myIntent);

}

对于某些 api 级别(日历视图带有指向顶部的切换月份的箭头),此 if 始终为 false,意图不会启动!

谁能告诉我为什么? 有什么方法可以让它工作,或者有什么方法可以让 CalendarView 的样式对所有 api 版本都一样吗?

这个解决方案解决了我的问题!

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_calendario);


    calendar = (CalendarView)findViewById(R.id.calendarView);
    date = calendar.getDate();

    calendar.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {

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

            int currentapiVersion = android.os.Build.VERSION.SDK_INT;

            if (currentapiVersion >= Build.VERSION_CODES.LOLLIPOP){
                // Do something for lollipop and above versions

                Intent myIntent = new Intent(CalendarioActivity.this, ActivityEvento.class);
                myIntent.putExtra("day",dayOfMonth).putExtra("month", month+1).putExtra("year", year);
                startActivity(myIntent);
            } else{
                // do something for phones running an SDK before lollipop

                /*With this if i block onSelectedDayChange when the mounth change*/
                if(calendar.getDate() != date) {
                    date = calendar.getDate();

                    Intent myIntent = new Intent(CalendarioActivity.this, ActivityEvento.class);
                    myIntent.putExtra("day",dayOfMonth).putExtra("month", month+1).putExtra("year", year);
                    startActivity(myIntent);

                }
            }

        }
    });
}

正如你在这里看到的,我检查 phone:

的版本
int currentapiVersion = android.os.Build.VERSION.SDK_INT;

                if (currentapiVersion >= Build.VERSION_CODES.LOLLIPOP){
                    // Do something for lollipop and above versions

                    Intent myIntent = new Intent(CalendarioActivity.this, ActivityEvento.class);
                    myIntent.putExtra("day",dayOfMonth).putExtra("month", month+1).putExtra("year", year);
                    startActivity(myIntent);
                } else{
                    // do something for phones running an SDK before lollipop

                    /*Con questo if blocco onSelectedDayChange quando viene scrollato
                    il calendario per cambiare mese*/
                    if(calendar.getDate() != date) {
                        date = calendar.getDate();
                   /* Quando seleziono un giorno apro l'activity per scriverci i compiti sopra*/
                    /*Passo tramite intent giorno,mese,anno*/
                        Intent myIntent = new Intent(CalendarioActivity.this, ActivityEvento.class);
                        myIntent.putExtra("day",dayOfMonth).putExtra("month", month+1).putExtra("year", year);
                        startActivity(myIntent);

                    }
                }

如果版本 >= LOLLIPOP,您只需要 take 事件的意图,而不检查用户是否滚动安装(不需要)。 但是如果版本是<你需要其他情况!