自定义日器选择器Dialog控件
allprojects {
repositories {
google()
jcenter()
//新添加的
maven { url 'https://jitpack.io' }
}
}
导入依赖
implementation 'com.github.prolificinteractive:material-calendarview:1.4.3'
xml文件中添加
<com.prolificinteractive.materialcalendarview.MaterialCalendarView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/calendarView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:mcv_showOtherDates="defaults"
app:mcv_selectionColor="#d862e2e4"
/>
运行就会出现基本的日历
下面来将日期做成dialog的形式 , 最终效果如下
新建 dialog_calendar_view.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"
android:padding="20dp">
<com.prolificinteractive.materialcalendarview.MaterialCalendarView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/calendarView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:mcv_selectionColor="#d862e2e4"
app:mcv_showOtherDates="defaults"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginBottom="5dp"
android:orientation="horizontal">
<Button
android:id="@+id/btn_cancle"
android:layout_width="0dp"
android:layout_height="25dp"
android:layout_marginRight="20dp"
android:layout_weight="1"
android:background="#00000000"
android:text="取消"
android:textSize="15sp" />
<Button
android:id="@+id/btn_ok"
android:layout_width="0dp"
android:layout_height="25dp"
android:layout_marginLeft="20dp"
android:layout_weight="1"
android:background="#00000000"
android:text="确定"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>
新建CalenerDialog Java类
package com.lanyu96.calendardialog.utils;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.lanyu96.calendardialog.R;
import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.CalendarMode;
import com.prolificinteractive.materialcalendarview.MaterialCalendarView;
import com.prolificinteractive.materialcalendarview.OnDateSelectedListener;
import java.util.Calendar;
import java.util.Date;
public class CalenerDialog extends Dialog {
private OnSelectedListener listener;
private MaterialCalendarView calendarView;
private CalendarDay calendarDay = null;
private Button btn_ok;
private Button btn_cancle;
public CalenerDialog(Context context) {
super(context);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_calendar_view);
setCanceledOnTouchOutside(false);
btn_ok = findViewById(R.id.btn_ok);
btn_cancle = findViewById(R.id.btn_cancle);
calendarView = findViewById(R.id.calendarView);
calendarView.state().edit()
.setFirstDayOfWeek(Calendar.MONDAY)
.setCalendarDisplayMode(CalendarMode.MONTHS)
.commit();
//设置默认选中的日器
calendarView.setSelectedDate(new Date());
calendarView.setCurrentDate(new Date());
calendarDay = calendarView.getSelectedDate();
//设置点击监听
calendarView.setOnDateChangedListener(new OnDateSelectedListener() {
@Override
public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date, boolean selected) {
calendarDay = date;
Toast.makeText(getContext(), ""+date, Toast.LENGTH_SHORT).show();
}
});
btn_ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (listener != null) {
if (calendarDay != null) {
listener.getData(calendarDay);
} else {
listener.getData(calendarDay);
}
} else {
listener.getData(calendarDay);
}
dismiss();
}
});
btn_cancle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dismiss();
}
});
}
public interface OnSelectedListener {
void getData(CalendarDay data);
}
public void setOnSelectedListener(OnSelectedListener listener) {
this.listener = listener;
}
}
然后新建一个测试的Demo来 调用日期选择控件
布局为一个简单的按钮控件
java代码为:
public class MainActivity extends AppCompatActivity {
private Button calendarBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
calendarBtn = findViewById(R.id.act_calendar_btn);
calendarBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CalenerDialog calenerDialog = new CalenerDialog(MainActivity.this);
calenerDialog.setOnSelectedListener(new CalenerDialog.OnSelectedListener() {
@Override
public void getData(CalendarDay data) {
Toast.makeText(MainActivity.this, ""+data.getDay()+"日", Toast.LENGTH_SHORT).show();
}
});
calenerDialog.show();
}
});
}
}