简体   繁体   English

从助手类调用 DatePicker 到 android 活动

[英]call DatePicker from helper class to an android activity

Iam using DatePickerDialog in one of my activity and its working fine but now I have to implement it in many activities so I thought to put it in a helper class and extend that function to all activities as good code practice.I have created an interface but couldn't able to use it properly.I have seen another link on stackoverflow but over there they are extending from one class to another class not activity.What I want is I have a helper class where I have put multiple function which Iam using repeatedly in my app and then extend them to any activity according to requirement.So I want to put this datepicker function in HelperClass then then get this function any where in app.Thanku我在我的一个活动中使用 DatePickerDialog 并且它工作正常但现在我必须在许多活动中实现它所以我想把它放在一个帮助程序类中并将该功能扩展到所有活动作为良好的代码实践。我已经创建了一个接口但是无法正确使用它。我在 stackoverflow 上看到了另一个链接,但在那里它们从一个类扩展到另一个类而不是活动。我想要的是我有一个辅助类,我在其中放置了多个我反复使用的函数在我的应用程序中,然后根据要求将它们扩展到任何活动。所以我想把这个日期选择器函数放在 HelperClass 中,然后在应用程序中的任何地方获取这个函数。谢谢

This is my datePicker function which is currently Iam using in an activity and its working fine这是我的 datePicker 函数,目前我正在一个活动中使用它并且工作正常

public void datePicker(int day,int month,int year) {
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
                    final Calendar myCalendar = Calendar.getInstance();
                    year = myCalendar.get(Calendar.YEAR);
                    month = myCalendar.get(Calendar.MONTH);
                    day = myCalendar.get(Calendar.DAY_OF_MONTH);
                }
                picker = new DatePickerDialog(RequestTraining.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {


                    }
                }, year, month, day);
                picker.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
                picker.show();
            }

This is the interface I made这是我做的界面

public interface DatePickerListener {

    public void onDate(DatePicker view, int year, int monthOfYear, int dayOfMonth);

}

I think, the best approach will be like this我认为,最好的方法是这样的

class Utils {
    public void datePicker(Context mContext, EditText et) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            final Calendar myCalendar = Calendar.getInstance();
            year = myCalendar.get(Calendar.YEAR);
            month = myCalendar.get(Calendar.MONTH);
            day = myCalendar.get(Calendar.DAY_OF_MONTH);
        }
        picker = new DatePickerDialog(mContext, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                et.setText(dayOfMonth + "/" + (monthOfYear + 1) + "/" + year);
            }
        }, year, month, day);
    picker.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
    picker.show();
   }
}

And now just call this method from any class like现在只需从任何类调用此方法

Utils.datePicker(this, editText);

Just Need to make an interface callBack and implement it in all of your activities where you need callBack.只需要制作一个接口回调并在您需要回调的所有活动中实现它。 Then you need to override the method onDateSelected(String date) in your activities which implements callBack interface.然后,您需要在实现callBack接口的活动中重写onDateSelected(String date)方法。 Just call the Utils.datePicker(this) where you want.只需在需要的地方调用Utils.datePicker(this) 即可

interface callBack{
onDateSelected(String date);
}
class Utils {
public static void datePicker(callBack call) {
                if (android.os.Build.VERSION.SDK_INT >= 
android.os.Build.VERSION_CODES.N) 
{
                    final Calendar myCalendar = Calendar.getInstance();
                year = myCalendar.get(Calendar.YEAR);
                month = myCalendar.get(Calendar.MONTH);
                day = myCalendar.get(Calendar.DAY_OF_MONTH);
            }
            picker = new DatePickerDialog(RequestTraining.this, new 
   DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear, int 
dayOfMonth) {
                    trainingDate = dayOfMonth + "/" + (monthOfYear + 1) + "/" + year;
                    call.onDateSelected(trainingDate);
                }
            }, year, month, day);
            picker.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
            picker.show();
        }
}

Hope it will help you.希望它能帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM