所以我正在尝试为低于 11 的 android 版本启用 datepicker。为此我正在使用支持库 v4。我导入所有必要的东西:
import android.support.v4.app.*;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.DialogFragment;
我创建了一个类:
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.EditText;
public class DatePicker extends DialogFragment implements DatePickerDialog.OnDateSetListener {
public EditText textField;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
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);
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public EditText getTextField() {
return textField;
}
public void setTextField(EditText textField) {
this.textField = textField;
}
public void onDateSet(DatePicker view, int year, int month, int day) {
textField.setText(day+"."+(month+1)+"."+year);
}
@Override
public void onDateSet(android.widget.DatePicker arg0, int arg1, int arg2,int arg3) {
textField.setText(arg3+"."+(arg2+1)+"."+arg1);
}
}
所以类编译好了。但问题是当我尝试使用它时。我有一个用于edittext的onclick方法,如下所示:
public void showDatePicker(View v) {
DialogFragment selectDate = (DialogFragment) new DatePicker();
EditText edit=(EditText)v;
((DatePicker) selectDate).setTextField(edit);
selectDate.show(getSupportFragmentManager(), "datePicker");
}
但是在最后一行我得到了错误:
The method getSupportFragmentManager() is undefined for the type MainActivity
任何想法如何解决这个问题?顺便说一句,我没有进口任何东西
android.app.Fragment;
所以这里不是这样:S