这是Android教程中的一个小例子,显示了DialogFragment和DatePicker。 它实现为MainActivity的静态内部类。我想知道是否可以更改代码以使其成为MainActivity的成员函数?
编辑:问题是我想在DialogFragment所在的内部类的MainActivity中设置textview。但是这个内部类是静态的 - 实际上是静态的。从静态开始,对主Activity的回调 - 回调函数 - 也必须是静态的。但是,如果此函数是静态的 - 如何在MainActivity中访问textview?
谢谢!
TextView tv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.dateTextView);
}
public void showTimePickerDialog(View v) {
newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
public static void notifyActivity() {
// want to change the textview here but cannot since the method is static.
}
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
private int year, month, day;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for 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);
DatePickerDialog date = new DatePickerDialog(getActivity(), DatePickerFragment.this, year, month, day);
date.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
notifyActivity();
}
});
return date;
}