Мой код работает нормально в Activity, но когда я использовал этот код во фрагменте, он не переопределяет метод Dialog onCreateDialog () , он показывает «Метод не переопределяет метод из itssuperclass».
TestFragment.java:
public class TestFragment extends DialogFragment implements View.OnClickListener{
private ImageView calendarOne_ime;
Context context;
private TextView tv_dateone;
private int myear;
private int mmonth;
private int mday;
static final int DATE_DIALOG_ID = 999;
TestFragment dialogFragment;
public TestFragment()
{
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootview = inflater.inflate(R.layout.activity_date_test, container, false);
FragmentManager fm = getFragmentManager();
dialogFragment = new TestFragment ();
calendarOne_ime = (ImageView)rootview. findViewById(R.id.cone);
tv_dateone = (TextView)rootview. findViewById(R.id.dateone_txt);
calendarOne_ime.setOnClickListener(this);
return rootview;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.cone:
setCurrentDateOnView();
addListenerOnButton();
break;
}
}
public void setCurrentDateOnView() {
final Calendar c = Calendar.getInstance();
myear = c.get(Calendar.YEAR);
mmonth = c.get(Calendar.MONTH);
mday = c.get(Calendar.DAY_OF_MONTH);
}
public void addListenerOnButton() {
getActivity().showDialog(DATE_DIALOG_ID);
}
@Override
public Dialog onCreateDialog(int id){
switch (id) {
case DATE_DIALOG_ID:
DatePickerDialog _date = new DatePickerDialog(getActivity(), datePickerListener, myear,mmonth, mday){
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
if (year < myear)
view.updateDate(myear, mmonth, mday);
if (monthOfYear < mmonth && year == myear)
view.updateDate(myear, mmonth, mday);
if (dayOfMonth < mday && year == myear && monthOfYear == mmonth)
view.updateDate(myear, mmonth, mday);
} };
return _date; }
return null;
}
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) {
myear = selectedYear;
mmonth = selectedMonth;
mday = selectedDay;
// set selected date into textview
// Toast.makeText(getApplicationContext(),new StringBuilder() .append(mmonth + 1).append("-").append(mday).append("-") .append(myear).append(" "),Toast.LENGTH_LONG).show();
tv_dateone.setText(new StringBuilder().append(mmonth + 1).append("/").append(mday).append("/").append(myear).append(" "));
}
};
}
xml код:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/dateviewone_layout"
android:layout_width="wrap_content"
android:layout_weight="1.2"
android:text="Area"
android:gravity="center_vertical"
android:layout_height="match_parent">
<TextView
android:id="@+id/dateone_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date"
android:gravity="center_vertical"
android:textStyle="bold"/>
<ImageView
android:id="@+id/cone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/appointment"
android:layout_alignParentRight="true"
android:layout_gravity="right"
/>
</RelativeLayout>
Я также делюсь снимком экрана, где я сталкиваюсь с проблемой.
Есть идеи, как ее решить?