无法使用 github 项目中的 material-calendarview 设置 onClick
Can't set onClick useing material-calendarview from github project
我参考了https://github.com/prolificinteractive/material-calendarview
我实现了 OnDateSelectedListener
和 @Override onDateSelected
,但是当我点击日历上的任何一天时都没有工作。
为什么?
任何人都可以教我我错过了什么?提前致谢
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>
我的日历class:
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();
}
}
}
您没有为 MaterialCalendarView 设置侦听器,要解决此问题,您必须在视图初始化后将此添加到 onCreate
:
calendarView.setOnDateChangedListener(this);
我参考了https://github.com/prolificinteractive/material-calendarview
我实现了 OnDateSelectedListener
和 @Override onDateSelected
,但是当我点击日历上的任何一天时都没有工作。
为什么?
任何人都可以教我我错过了什么?提前致谢
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>
我的日历class:
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();
}
}
}
您没有为 MaterialCalendarView 设置侦听器,要解决此问题,您必须在视图初始化后将此添加到 onCreate
:
calendarView.setOnDateChangedListener(this);