- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个名为 DatePicker
的自定义类,它扩展了 DialogFragment
并实现了 DatePickerDialog.OnDateSetListener
,但我的问题是因为您需要监听onDateChangeMethod
和 DialogFragment
只有 DialogInterface.OnCancelListener
和 DialogInterface.OnDismissListener
。
我怎样才能做到这一点?我尝试实现 OnDateChangedListener 并重写 onDateChange 方法,但它不起作用。这是代码:
public class DatePicker extends DialogFragment implements
DatePickerDialog.OnDateSetListener, OnDateChangedListener {
private OnDateSetListener _onDateSetListener;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
_onDateSetListener = (OnDateSetListener) activity;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Date initialValue = null;
String[] buttons = getArguments().getStringArray("buttons");
Long initialValueLong = getArguments().getLong("initialValue");
if (initialValue == null) {
initialValue = new Date();
}
Calendar calendar = GregorianCalendar.getInstance();
calendar.setTimeInMillis(initialValueLong);
DatePickerDialog dialog = new DatePickerDialog(getActivity(), this,
calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
calendar.get(Calendar.DATE));
return dialog;
}
@Override
public void onDateSet(android.widget.DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
_onDateSetListener.onDateSet(new GregorianCalendar(year, monthOfYear,
dayOfMonth).getTime(), getArguments().get("tag"));
}
@Override
public void onDateChanged(android.widget.DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// THIS METHOD NEVER CALLED
}
}
最佳答案
您似乎尚未在类中声明 onDateSetListener,并且您还将主机 Activity 转换为错误的类型。 Dialog 类本身应该是 OnDateSetListener,并且主机 Activity 应该转换为其他内容。
尝试像这样构造类:
public class DatePickerDialogFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener
{
public static interface DatePickedListener
{
public void onDatePicked(int selectedYear, int selectedMonth, int selectedDay);
}
private DatePickedListener listener;
@Override
public void onAttach(Activity activity)
{
// when the fragment is initially attached to the activity, cast the
// activity to the callback interface type
super.onAttach(activity);
try
{
listener = (DatePickedListener) activity;
}
catch (ClassCastException e)
{
throw new ClassCastException(activity.toString() + " must implement " + DatePickedListener.class.getName());
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
Bundle b = getArguments();
int year = b.getInt("set_year");
int month = b.getInt("set_month");
int day = b.getInt("set_day");
return new DatePickerDialog(getActivity(), this, year, month, day);
}
@Override
public void onDateSet(DatePicker view, int setYear, int setMonth, int setDay)
{
// when the date is selected, send it to the activity via its callback interface method
listener.onDatePicked(setYear, setMonth, setDay);
}
}
关于java - 当我有 MyCustomClass 扩展 DialogFragment 时如何实现 onDatePickerChanged,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24738890/
我有一个 AppCompatActivity,它在某个时候显示一个 DialogFragment。在此对话框中,有些项目在删除之前需要确认。通过另一个 Yes/No DialogFragment 询问
我们添加一个通用的/普通的 fragment ,以编程的方式做一些事情,比如: fragmentTransaction.add(containerViewId, fragmentToAdd, frag
我有一个 DialogFragment 向用户显示选项列表,其中一个选项是“删除”选项,当用户按下删除选项时我想显示另一个 DialogFragment 作为确认,不幸的是,确认对话框没有显示。 这是
假设我在一个名为 my_dialog_fragment.xml 的 xml 布局文件中指定了 DialogFragment 的布局,并指定了 layout_width 和 >layout_height
当我设置对话框 fragment 的样式时,android 允许截取对话框 fragment 的屏幕截图,但在删除样式后它会起作用。 下面是对话框 fragment 的onCreate代码 @Over
我的应用程序中有一个带有日期和时间字段的 DialogFragment1(由 MyFragmentActivity 调用)。当用户单击它们时,会出现 DatePickerFragment 和 Time
显示 ProgressDialog 时应用程序崩溃。我正在使用滑出式键盘在手机上进行测试。显示对话框时,我滑动键盘,应用崩溃。 public static class ProgressDialogFr
正如问题中提到的.. 我有一个 fragment ,我从中创建并显示一个 DialogFragment,并将 targetFragment 设置为此当前 fragment 。 DialogFragme
如何在此 DialogFragment 中检索 editText1?它存在于 vraag_banen.xml 中,但 getView() 为空。 该对话框显示正常,也没有编译错误,但是我无法弄清楚如何
我应该如何创造这个设计的优越部分? 我正在使用 DialogFragment 在弹出窗口中执行此操作,但无法实现顶部透明的效果。 按钮设计但 background_circle: 使用
最后,我发现问题是什么...... 如果你有这样的功能: public void test() { DialogFragment dlg = new LoginDialog(); dl
我有一个带有监听器的 DialogFragment,用于单击按钮以调用 fragment 中的函数。 我收到 lateinit property listener has not been initi
我在 Activity 上有一个 DialogFragment。此 DialogFragment 是一个由 Web 服务调用填充的 ListView 。单击此 ListView 中的一行后,对话框应消
我正在尝试遵循本教程:http://developer.android.com/guide/topics/ui/controls/pickers.html#DatePicker 这是他们提供的代码,所
有什么方法可以更改 DialogFragment 中正面、负面和中性按钮的显示顺序,以便我可以先放置负面按钮? 我不想改变按钮的“性质”,而是保持它们为“积极”、“消极”和“中性”。 最佳答案 尝试这
这里可能重复,抱歉... 我不知道如何将自定义样式添加到我的 DialogFragment。此刻我正在上课 public final class SelectFragment extends Dial
我花了几个小时来让这个简单的用例正常工作:当我按下音量硬件按钮时,我想打开一个 DialogFragment。该对话框包含一个 SeekBar,应根据按下的按钮进行设置。 我这里有几个问题。 首先,我
DialogFramgment 有问题(在多个 Android 4.2/4.4 设备上支持 v4 库)。 我有两个 DialogFragment:EditAccountDialogFragment 和
当我将有效代码从 fragment 复制到dialogfragment时,它突然不起作用。 在常规 fragment 上:将位图保存在 sd 卡上。 on dialogfragment: 不将位图保存
我花了一整天的时间试图弥补这一点,但我不能.. 这就是问题所在:我想要一个是/否 AlertDialog方向改变时不会消失,所以我决定使用 DialogFragment . 所以我准备了代码并第一次使
我是一名优秀的程序员,十分优秀!