识别对话调用者的最佳方法是什么?

时间:2015-12-09 09:56:24

标签: android android-fragments android-dialogfragment android-bundle android-number-picker

我有一个包含10个以上字段的表单片段。在点击事件中,每个人都会显示一个数字选择器对话框。现在,我如何识别哪个字段在对话框中生成了值选择?在片段中,由于接口实现,我只有一个回调:

public interface DialogClickListener {
    void onDialogClick(DialogFragment dialog, Bundle args);
}

我用以下内容实例化对话框:

private DialogClickListener clickListener;

public static NumberPickerDialogFragment newInstance(final int fiedlId) {
    NumberPickerDialogFragment dialog = new NumberPickerDialogFragment();

    // Here I can pass arguments to dialog with bundle
    Bundle bundle = new Bundle();
    bundle.putInt("fieldid", fieldId);
    dialog.setArguments(bundle);

    return dialog;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Here I can get the arguments
    final int fieldId = getArguments().getInt("fieldid");

    /* Here I can send the id back to fragment callback dialogclick
     * This happen for real inside positive dialog button
    */
    final Bundle bundle = new Bundle();
    bundle.putInt("fieldid", fieldid);
    clicklistener.onDialogClick(NumberPickerDialogFragment.this, bundle);
}

基本上我将字段id发送到对话框,然后将其发送回片段回调:这样我就可以在片段内部切换id。我认为这个解决方案对于它的目标来说有点过于复杂和冗长。你能建议一个替代方案吗?非常感谢提前。

0 个答案:

没有答案