问题
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;
}
回答1:
I ran into a similar problem. I wanted to use an AlertDialog Dialog Fragment in multiple activities.
Here is my DialogFragment which is made of an Alert Dialog. In newInstance I require the title and the activity. Title is going to be in string.xml. Activity is going to be 1 or 2 based on what I say when I create a new MyDialog. I pass both of those to the Bundle "args" using putInt.(You can also use putString if you are more comfortable with that. Then in the onCreateDialog, I pull both the title and the activity name from getArguments() and use the activity to decided in a switch which action to take. And of course based on which Activity I am trying to do the .doPositiveClick or .doNegativeClick, I have to cast which Activity to perform the function in. Using this method, I can make minimal changes to MyDialog class and use it in other Activities.
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();
}
}
Here is where I call on my alert dialog. This can be used in two different Activities. "MyActivity" and "AnotherActivity"
void showDeleteDialog() {
DialogFragment newFragment = MyDialog.newInstance(
R.string.myDialogTitle, 1);
newFragment.show(getFragmentManager(), "dialog");
}
I hope this helps. Let me know if you need more clarification.
回答2:
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
来源:https://stackoverflow.com/questions/12114436/datepicker-dialog-using-dialogfragment-for-multiple-activities