写点什么

DatePickerDialog 时间选择器 +MVPPlugin 开发插件的使用

用户头像
Android架构
关注
发布于: 1 小时前

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


《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
浏览器打开:qq.cn.hn/FTe 免费领取
复制代码


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(),"选择日期");


}


});

用户头像

Android架构

关注

还未添加个人签名 2021.10.31 加入

还未添加个人简介

评论

发布
暂无评论
DatePickerDialog时间选择器+MVPPlugin开发插件的使用