选择 DatePickerDialog 时对话框 onCreateDialog 显示错误
Dialog onCreateDialog show error when pick up the DatePickerDialog
我的代码在 Activity 中运行良好,但是当我在片段中使用此代码时,它不会覆盖方法 Dialog onCreateDialog() ,它显示 'Method does not override the method from 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>
我也分享了我遇到问题的屏幕截图。
知道如何解决吗?
使用这个
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.support.v4.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.TextView;
import com.pickcel.lite.agent.R;
import java.util.Calendar;
/**
* A simple {@link Fragment} subclass.
*/
public class TestFragment extends Fragment implements View.OnClickListener{
private ImageView calendarOne_ime;
Context context;
public static TextView tv_dateone;
public TestFragment()
{
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.fragment_payment, container, false);
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:
android.support.v4.app.DialogFragment newFragment = new SelectDateFragment();
newFragment.show(getFragmentManager(), "DatePicker");
break;
}
}
public static class SelectDateFragment extends android.support.v4.app.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);
}
public void populateSetDate(int year, int month, int day) {
tv_dateone.setText(month+"/"+day+"/"+year);
}
}
}
输出:
制作对话框片段 abstract.Follow 下面的代码:
public abstract class DialogFragment extends Fragment
对于日期选择器,请遵循以下代码:
只需要发送fragment/activity和textview/editext的context
您需要打开日期选择器。
public static void setDateFromDatePicker(Context context, TextView textView) {
int mYear, mMonth, mDay;
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
// Launch Date Picker Dialog
DatePickerDialog dpd = new DatePickerDialog(context,
(view, year, monthOfYear, dayOfMonth) -> {
Calendar calendar = Calendar.getInstance();
calendar.set(year, monthOfYear, dayOfMonth);
SimpleDateFormat sdf = new SimpleDateFormat(Constants.LOCAL_DATE_FORMAT, Locale.ENGLISH);
String formattedDate = sdf.format(calendar.getTime());
textView.setText(formattedDate);
// textView.setText(String.valueOf(dayOfMonth) + "/" + (monthOfYear + 1) + "/" + year);
}, mYear, mMonth, mDay);
dpd.show();
}
我的代码在 Activity 中运行良好,但是当我在片段中使用此代码时,它不会覆盖方法 Dialog onCreateDialog() ,它显示 'Method does not override the method from 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>
我也分享了我遇到问题的屏幕截图。
知道如何解决吗?
使用这个
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.support.v4.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.TextView;
import com.pickcel.lite.agent.R;
import java.util.Calendar;
/**
* A simple {@link Fragment} subclass.
*/
public class TestFragment extends Fragment implements View.OnClickListener{
private ImageView calendarOne_ime;
Context context;
public static TextView tv_dateone;
public TestFragment()
{
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.fragment_payment, container, false);
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:
android.support.v4.app.DialogFragment newFragment = new SelectDateFragment();
newFragment.show(getFragmentManager(), "DatePicker");
break;
}
}
public static class SelectDateFragment extends android.support.v4.app.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);
}
public void populateSetDate(int year, int month, int day) {
tv_dateone.setText(month+"/"+day+"/"+year);
}
}
}
输出:
制作对话框片段 abstract.Follow 下面的代码:
public abstract class DialogFragment extends Fragment
对于日期选择器,请遵循以下代码:
只需要发送fragment/activity和textview/editext的context 您需要打开日期选择器。
public static void setDateFromDatePicker(Context context, TextView textView) {
int mYear, mMonth, mDay;
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
// Launch Date Picker Dialog
DatePickerDialog dpd = new DatePickerDialog(context,
(view, year, monthOfYear, dayOfMonth) -> {
Calendar calendar = Calendar.getInstance();
calendar.set(year, monthOfYear, dayOfMonth);
SimpleDateFormat sdf = new SimpleDateFormat(Constants.LOCAL_DATE_FORMAT, Locale.ENGLISH);
String formattedDate = sdf.format(calendar.getTime());
textView.setText(formattedDate);
// textView.setText(String.valueOf(dayOfMonth) + "/" + (monthOfYear + 1) + "/" + year);
}, mYear, mMonth, mDay);
dpd.show();
}