所以我試圖啓用datepicker android版本波紋管11.爲此,我正在使用支持庫v4。我導入所有必要的東西:'getSupportFragmentManager()'方法未被導入
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);
}
}
因此類編譯OK。但問題是,當我嘗試使用它。我有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
你實際上應該接受別人表明這個答案是解決問題 –
所以我做了,幸運的是,我通過了,現在:d – gabrjan