DatePickerDialog.OnDateSetListener() 和 MVP
DatePickerDialog.OnDateSetListener() and MVP
我很困惑...我正在尝试学习 MVP 模式,但我在该代码中的实现遇到了问题:
mDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
month = month + 1;
Log.d(TAG, "onDataSet: dd/mm/yyyy: " + year + "/" + month + "/" + dayOfMonth);
String date = dayOfMonth + "/" + month + "/" + year;
dateText.setText(date);
}
};
和:
@OnClick(R.id.date_text)
public void onClickDate() {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(getActivity(), android.R.style.Theme_Holo_Light_Dialog_MinWidth, mDateSetListener,
year, month, day);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
}
谁能告诉我如何在 MVP 中正确编写这段代码?
我知道它是如何工作的,但是这个例子对于初学者来说有点难以理解。
我将采用的方式是:
定义视图的界面(Activity)
public interface MyView {
public void setDateText (String date);
public void displayDatePickerDialog(int year, int month, int day);
}
那么 Presenter 会是这样的:
public class MyPresenter {
MyView view;
public MyPresenter (MyView view){
this.view = view;
}
public void onDateSet(int year, int month, int dayOfMonth){
month = month + 1;
String date = dayOfMonth + "/" + month + "/" + year;
view.setDateText(date);
}
public void onDateClicked (){
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
view.displayDatePickerDialog(year, month, day);
}
你的 activity 应该看起来像这样:
public class MainActivity extends AppCompatActivity implements MyView{
MyPresenter myPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
myPresenter = new MyPresenter(this);
}
@OnClick(R.id.date_text)
public void onClickDate() {
myPresenter.onDateClicked();
}
mDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
myPresenter.onDateSet(year, month, dayOfMonth);
}
};
@Override
public void setDateText (String date){
dateText.setText(date);
}
@Override
public void displayDatePickerDialog(int year, int month, int day){
DatePickerDialog dialog = new DatePickerDialog(getActivity(), android.R.style.Theme_Holo_Light_Dialog_MinWidth, mDateSetListener,
year, month, day);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
}
}
我很困惑...我正在尝试学习 MVP 模式,但我在该代码中的实现遇到了问题:
mDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
month = month + 1;
Log.d(TAG, "onDataSet: dd/mm/yyyy: " + year + "/" + month + "/" + dayOfMonth);
String date = dayOfMonth + "/" + month + "/" + year;
dateText.setText(date);
}
};
和:
@OnClick(R.id.date_text)
public void onClickDate() {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(getActivity(), android.R.style.Theme_Holo_Light_Dialog_MinWidth, mDateSetListener,
year, month, day);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
}
谁能告诉我如何在 MVP 中正确编写这段代码? 我知道它是如何工作的,但是这个例子对于初学者来说有点难以理解。
我将采用的方式是:
定义视图的界面(Activity)
public interface MyView {
public void setDateText (String date);
public void displayDatePickerDialog(int year, int month, int day);
}
那么 Presenter 会是这样的:
public class MyPresenter {
MyView view;
public MyPresenter (MyView view){
this.view = view;
}
public void onDateSet(int year, int month, int dayOfMonth){
month = month + 1;
String date = dayOfMonth + "/" + month + "/" + year;
view.setDateText(date);
}
public void onDateClicked (){
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
view.displayDatePickerDialog(year, month, day);
}
你的 activity 应该看起来像这样:
public class MainActivity extends AppCompatActivity implements MyView{
MyPresenter myPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
myPresenter = new MyPresenter(this);
}
@OnClick(R.id.date_text)
public void onClickDate() {
myPresenter.onDateClicked();
}
mDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
myPresenter.onDateSet(year, month, dayOfMonth);
}
};
@Override
public void setDateText (String date){
dateText.setText(date);
}
@Override
public void displayDatePickerDialog(int year, int month, int day){
DatePickerDialog dialog = new DatePickerDialog(getActivity(), android.R.style.Theme_Holo_Light_Dialog_MinWidth, mDateSetListener,
year, month, day);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
}
}