DatePickerDialog 时间选择器 +MVPPlugin 开发插件的使用
public Button getMonthButton() {
return monthBtn;
}
@Override
public Activity getActivity() {
return MainActivity.this;
}
}
2.MainContract.class 类中,主要在接口中定义我们想要的方法,示例代码如下:
/**
MVPPlugin
主要在接口中定义出我们自己想要的方法
*/
public class MainContract {
interface View extends BaseView {
Button getYearButton();
Button getMonthButton();
Activity getActivity();
}
interface Presenter extends BasePresenter<View> {
void setView();
void onClickBtn();
}
}
3.MainPresenter.class 是主要负责 MainActivity 的业务逻辑处理,来分担 view 层的压力
/**
MVPPlugin
MainActivity 的业务逻辑处理
*/
public class MainPresenter extends BasePresenterImpl<MainContract.View> implements MainContract.Presenter{
Button yearBtn,monthBtn;
Activity mActivity;
Calendar calendar_selected = Calendar.getInstance();
@Override
public void setView() {
yearBtn = mView.getYearButton();
monthBtn = mView.getMonthButton();
mActivity = mView.getActivity();
}
@Override
public void onClickBtn() {
/**
时间选择器(选择的年份)
*/
yearBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int year = calendar_selected.get(Calendar.YEAR);
int month = calendar_selected.get(Calendar.MONTH);
int day = calendar_selected.get(Calendar.DAY_OF_MONTH);
//时间选择器(选择年份)
MyDatePickerDialog myDatePickerDialog = MyDatePickerDialog.newInstance(new MyDatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(MyDatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
Toast.makeText(mActivity,year+"",Toast.LENGTH_LONG).show();
}
},year, month, day);
myDatePickerDialog.showYearPickerFirst(true);
myDatePickerDialog.setMaxDate(Calendar.getInstance());
myDatePickerDialog.setTitle("选择年份");
myDatePickerDialog.show(mActivity.getFragmentManager(), "myDatePickerDialog");
}
});
/**
时间选择器(选择的月份)
*/
monthBtn.se
tOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int year = calendar_selected.get(Calendar.YEAR);
int month = calendar_selected.get(Calendar.MONTH);
int day = calendar_selected.get(Calendar.DAY_OF_MONTH);
//时间选择器(选择年份)
MyDatePickerDialog myDatePickerDialog = MyDatePickerDialog.newInstance(new MyDatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(MyDatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
Toast.makeText(mActivity,year+"-"+(monthOfYear+1)+"-"+dayOfMonth,Toast.LENGTH_LONG).show();
}
},year, month, day);
myDatePickerDialog.showYearPickerFirst(false);
myDatePickerDialog.setMaxDate(Calendar.getInstance());
myDatePickerDialog.setTitle("选择年月日");
myDatePickerDialog.show(mActivity.getFragmentManager(), "myDatePickerDialog");
}
});
/**
时间选择器(选择的年月日)
*/
allBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DatePickerDialog datePickerDialog = DatePickerDialog.newInstance(
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
calendar_selected.set(Calendar.YEAR,year);
calendar_selected.set(Calendar.MONTH,monthOfYear);
calendar_selected.set(Calendar.DAY_OF_MONTH,dayOfMonth);
Toast.makeText(mActivity,year+"-"+(monthOfYear+1)+"-"+dayOfMonth,Toast.LENGTH_LONG).show();
}
},
calendar_selected.get(Calendar.YEAR),
calendar_selected.get(Calendar.MONTH),
calendar_selected.get(Calendar.DAY_OF_MONTH));
datePickerDialog.setTitle("请选择日期");
datePickerDialog.setMaxDate(Calendar.getInstance());
datePickerDialog.show(mActivity.getFragmentManager(),"选择日期");
}
});
评论