Access中的FragmentActivity由DialogFragment扩展

时间:2013-04-22 09:42:02

标签: android-fragmentactivity android-dialogfragment android-datepicker

我有一个扩展的类DialogFragment。我想在FragmentActivity中的DialogFragment之类的扩展类中调用方法。

在我的FragmentActivity中,我按以下方式拨打DialogFragment

DialogFragment dialogFragment = new MyDialogFragmentClass();
dialogFragment.show(getSupportFragmentManager(), "datePicker");

我正在使用此DialogFragment来显示DatePicker。当用户选择日期并点击设置按钮时,我想在我的FragmentActivity中调用方法。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

public class MainActivity extends FragmentActivity {

EditText mEdit;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void selectDate(View view) {
    DialogFragment newFragment = new SelectDateFragment();
    newFragment.show(getSupportFragmentManager(), "DatePicker");
}
public void populateSetDate(int year, int month, int day) {
    mEdit = (EditText)findViewById(R.id.editText1);
    mEdit.setText(month+"/"+day+"/"+year);
}
public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Calendar calendar = Calendar.getInstance();
        int yy = calendar.get(Calendar.YEAR);
        int mm = calendar.get(Calendar.MONTH);
        int dd = calendar.get(Calendar.DAY_OF_MONTH);
        return new DatePickerDialog(getActivity(), this, yy, mm, dd);
    }

    public void onDateSet(DatePicker view, int yy, int mm, int dd) {
        populateSetDate(yy, mm+1, dd);
    }
}

}

//导入必要的包......