如何设置从DatePicker的对话框中的呼叫日期 [英] How to set the date from DatePicker dialog for multiple calls
问题描述
美好的一天,似乎无法找到一个解决方案。我有2个按钮,当pressed,打开一个datepickerdialog,但我现在的问题是,我怎么能设置他们在OnDateSet方法的相应按钮。我使用的日期DialogFragment,然后实施我的活动DateListener。我一直在使用getTag(),但在获取标签没有成功尝试。这里是我的尝试:
Good day, can't seem to find a solution for this. i have 2 buttons that when pressed, opens a datepickerdialog, but my problem now is , how can i set them to the appropriate button on OnDateSet method. I am using a DialogFragment for the date and then implementing the DateListener in my activity. I have tried using getTag() but no success in getting the tag. here is what i tried:
public void showfromDatePickerDialog(View v) {
DialogFragment dateFragment = new DateDialogFragment();
dateFragment.show(getSupportFragmentManager(), "fromdatePicker");
}
public void showtoDatePickerDialog(View v) {
DialogFragment dateFragment = new DateDialogFragment();
dateFragment.show(getSupportFragmentManager(), "todatePicker");
}
public void onDateSet(DatePicker view, int year, int month, int day) {
StringBuilder builder = new StringBuilder();
builder.append(day).append("-")
.append(month).append("-")
.append(year);
String text= builder.toString();
if(view.getTag().toString().equals("fromdatePicker")) { // error here
Log.d(TAG, "got here" + text);
fromdate.setText(text);
}
if(view.getTag() == "todatePicker") {
todate.setText(text);
}
任何想法如何实现这一点?我不断看到有关使用2种不同的DialogFragment一流的解决方案,但我猜测应该有另一种方式。还是我错了?谢谢
any ideas how to implement this? i keep seeing solutions about using 2 different DialogFragment class but am guessing there should be another way. or am i wrong? Thank you
推荐答案
好吧,我对这些周围的工作。它可以是对这个问题的人的帮助。如果稍有不正确,请让我知道,我可以改变它。但是这是我做过什么来解决这个问题。
ok, i have a work around for these. which can be helpful for anyone with this problem. if its slightly incorrect please let me know and i can change it. but this is what i did to solve this issue.
在DateSet:
public void onDateSet(DatePicker view, int year, int month, int day) {
StringBuilder builder = new StringBuilder();
builder.append(day).append("-")
.append(month).append("-")
.append(year);
String text= builder.toString();
FragmentManager fragmanager = getSupportFragmentManager();
if(fragmanager.findFragmentByTag("fromdatePicker") != null) {
Log.d(TAG, "got here" + text);
fromdate.setText(text);
}
// if(view.getTag() == "todatePicker") {
if(fragmanager.findFragmentByTag("todatePicker") != null) {
todate.setText(text);
}
这样你可以使用多个调用同一dateListener并设置适当的日期基础上调用对话框中显示时所通过的标签。
that way you can use the same dateListener for multiple calls and set the date appropriately based on the tag that was passed when calling show on the dialog.
这篇关于如何设置从DatePicker的对话框中的呼叫日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!