0

I need to use DatePicker dialog using the new DialogFragment class for multiple activities. I am having more than two activities using datepicker and time picker. I succeeded using the example in developer.android.com for Dialog Fragment for single activity.

public class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
    String date;
    Bundle bundle;
    Intent in;
    //private View v;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);



// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(),(Pickup)getActivity(), year, month, day);

}

public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
    // TODO Auto-generated method stub

}

If I use that I have to write different DialogFragment classes for each activities. Is there any solution like using switch case provided in this example?

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addnewreminder);
    initialize();
    context = getApplicationContext();
    OnClickListener listenerDate = new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            final Calendar c = Calendar.getInstance();
            year = c.get(Calendar.YEAR);
            month = c.get(Calendar.MONTH);
            day = c.get(Calendar.DAY_OF_MONTH);
            showDialog(DATE_DIALOG_ID);
        }
    };
    editTextDate.setOnClickListener(listenerDate);

}

private void initialize() {
    // TODO Auto-generated method stub

    editTextDate = (EditText) findViewById(R.id.editTextDate);

}


private void updateDisplay() {
    currentDate = new StringBuilder().append(day).append(".")
            .append(month + 1).append(".").append(year).toString();

    Log.i("DATE", currentDate);
}

OnDateSetListener myDateSetListener = new OnDateSetListener() {

    @Override
    public void onDateSet(DatePicker datePicker, int i, int j, int k) {

        year = i;
        month = j;
        day = k;
        updateDisplay();
        editTextDate.setText(currentDate);
    }
};



@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DATE_DIALOG_ID:
        return new DatePickerDialog(this, myDateSetListener, year, month,
                day);
    }
    return null;
}
4

2 回答 2

0

我遇到了类似的问题。我想在多个活动中使用 AlertDialog 对话框片段。

这是我的 DialogFragment,它由警报对话框组成。在 newInstance 中,我需要标题和活动。标题将在 string.xml 中。根据我在创建新 MyDialog 时所说的话,活动将是 1 或 2。我使用 putInt 将这两个都传递给 Bundle “args”。(如果您对此更满意,也可以使用 putString。然后在 onCreateDialog 中,我从 getArguments() 中提取标题和活动名称并使用活动决定在开关中采取哪个动作。当然,根据我尝试执行 .doPositiveClick 或 .doNegativeClick 的哪个 Activity,我必须强制转换哪个 Activity 来执行该功能。使用这种方法,我可以进行最小的更改到 MyDialog 类并在其他活动中使用它。

public class MyDialog extends DialogFragment {

static MyDialog newInstance(int title, int activity) {
    MyDialog dialog = new MyDialog();
    Bundle args = new Bundle();
    args.putInt("title", title);
    args.putInt("activity", activity);
    dialog.setArguments(args);
    return dialog;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the Builder class for convenient dialog construction
    int title = getArguments().getInt("title");
    int activity = getArguments().getInt("activity");
    return new AlertDialog.Builder(getActivity())
    .setTitle(title)
    .setPositiveButton(R.string.yes,
            new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            switch (activity){

            case 1:
                ((MyActivity) getActivity()).doPositiveClick();
                break;
            case 2:
                ((AnotherActivity) getActivity()).doPositiveClick();
                break;
            }
        }
    }
    )
    .setNegativeButton(R.string.cancel,
            new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            switch (activity) {
            case 1:
                ((MyActivity)getActivity()).doNegativeClick();
                break;
            case 2:
                ((AnotherActivity)getActivity()).doNegativeClick();
                break;
                    }
                }
            }
    )
    .create();
}

}

这是我调用警报对话框的地方。这可以在两个不同的活动中使用。“我的活动”和“另一个活动”

    void showDeleteDialog() {
    DialogFragment newFragment = MyDialog.newInstance(
            R.string.myDialogTitle, 1);
    newFragment.show(getFragmentManager(), "dialog");
}

我希望这有帮助。如果您需要更多说明,请告诉我。

于 2013-01-25T17:54:35.170 回答
0

来晚了,但我希望这个答案对将来的某个人有所帮助:我通过 setArgument 方法为每个活动传递 DIALOG_ID 参数并在 dialogFragment 中检查 DIALOG_ID 值并为每个活动设置 pickerDialog :{

public Dialog onCreateDialog(Bundle savedInstanceState) {
    if (dialog_id == 1) {
        return new DatePickerDialog(getActivity(), ondateSet, year, month, day);
    } else {
        return new TimePickerDialog(getContext(), ontimeSet, hour, minute, false);
    }
}

}

喜欢这个片段示例以更清楚地理解它

于 2017-06-18T12:59:13.003 回答