2017-05-04 79 views
1

设置的onClick期运用材料calendarview我从https://github.com/prolificinteractive/material-calendarview不能从GitHub项目

我实现OnDateSelectedListener@Override onDateSelected参考,但没有工作,当我clcik日历天任。

为什么?

任何人都可以教我点我错过了什么?在此先感谢

gradle这个项目:

compile 'com.prolificinteractive:material-calendarview:1.4.3' 

布局的xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.user.testcalendar0420_1.MainActivity"> 

    <com.prolificinteractive.materialcalendarview.MaterialCalendarView 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/calendarView" 
     android:layout_width="368dp" 
     android:layout_height="wrap_content" 
     app:mcv_showOtherDates="all" 
     app:mcv_selectionColor="#00F" 
     tools:layout_editor_absoluteY="0dp" 
     tools:layout_editor_absoluteX="8dp" /> 

</LinearLayout> 

我的日历类:

public class MainActivity extends AppCompatActivity implements OnDateSelectedListener { 

    private MaterialCalendarView calendarView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     //I can see the calendar on screen 
     calendarView = (MaterialCalendarView) findViewById(R.id.calendarView); 

     System.out.println("onCreate"); 

    } 

    @Override 
    public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date, boolean selected) { 
     //selected is no value on logcat 
     Log.d("selected", "" + selected); 
     //It can't be show 
     Toast.makeText(this, "enterDateSelected" + date, Toast.LENGTH_SHORT).show(); 

     if (selected == true) { 
      //It can't be show 
      Toast.makeText(this, "onClick" + date, Toast.LENGTH_SHORT).show(); 
     } 
    } 

} 

回答

1

你是不是听者设置为MaterialCalendarView,解决它必须在视图初始化后将其添加到onCreate

calendarView.setOnDateChangedListener(this); 
+0

圣...我怎么能错过代码,你救了我的命,谢谢。 –