0

我有一个扩展DialogFragment. 我想调用一个在 this中扩展的类中的方法。FragmentActivityDialogFragment

在我的FragmentActivity,我做如下调用DialogFragment

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

我用它DialogFragment来展示一个DatePicker. 当用户选择日期并单击设置按钮时,我想在我的FragmentActivity.

我怎样才能做到这一点 ?

4

1 回答 1

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

}

// 导入必要的包...

于 2013-06-19T09:35:32.883 回答