拾取DatePickerDialog时,对话框onCreateDialog显示错误

时间:2018-04-25 05:30:45

标签: android android-fragments android-datepicker

我的代码在Activity中运行正常,但是当我在片段中使用此代码时,它不会覆盖方法Dialog onCreateDialog(),它显示'方法不会覆盖itssuperclass中的方法'。

TestFragment.java:

val test = Fifo.isEmpty

xml代码:

org.apache.spark.sql.Dataset.drop(String... colNames)

我也分享了我遇到问题的屏幕截图。enter image description here

知道怎么解决吗?

2 个答案:

答案 0 :(得分:2)

使用此

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);
    }

   }
}

<强>输出:
     enter image description here

enter image description here

答案 1 :(得分:0)

制作Dialog片段摘要。遵循代码:

public abstract class DialogFragment extends Fragment

对于Datepicker,请按照以下代码:

只需要发送片段/活动和textview / editext的上下文  你需要打开日期选择器。

  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();
    }