使用DialogFragment进行多个活动的datepicker对话框 [英] datepicker dialog using DialogFragment for multiple activities

查看:112
本文介绍了使用DialogFragment进行多个活动的datepicker对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用DatePicker对话框,并使用新的DialogFragment类进行多个活动。
我使用datepicker和时间选择器进行了两个以上的活动。我已成功在developer.android.com中将示例用于单个活动的Dialog Fragment。

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

}

如果使用该命令,则必须为每个活动编写不同的DialogFragment类。

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;
}


推荐答案

我很晚了,但我希望此答案将对以后的工作有所帮助:
i可以通过setArgument方法为每个活动传递DIALOG_ID参数来进行操作,并在dialogFragment中检查DIALOG_ID值并为每个
设置pickerDialog例如:
{

am to late but i hope this answer will help some one in future : i do'it by passing DIALOG_ID argument by setArgument method for each activity and in dialogFragment check the DIALOG_ID value and set pickerDialog for each one ex: {

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);
    }
}

}

喜欢此帖片段示例以使其更加清晰

like this snippet example to under stand it more clear

这篇关于使用DialogFragment进行多个活动的datepicker对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆