【问题标题】:DatePicker Inside a Fragment片段内的日期选择器
【发布时间】:2015-10-08 09:44:23
【问题描述】:

您好,我是 android 开发的新手。我有问题。 我有一个片段。在片段视图中,我得到了日期选择器的图像按钮。运行代码时没有发现错误。但是当我单击图像按钮时,日期选择器对话框没有显示..

请任何人帮助我。

代码如下:

public class Payment05TowMachineEntryFragment extends Fragment {

    public static MainActivity lastCreated;

    private TextView DateDisplay;
    private ImageButton PickDate;
    private Calendar Date;

    static final int DATE_DIALOG_ID = 999;

    private TextView activeDateDisplay;
    private Calendar activeDate;

    public Payment05TowMachineEntryFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.payment05_towmachine_entry_fragment, container, false);

        return rootView;
    }

    @Override
    public void onActivityCreated(final Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        DateDisplay = (TextView) (getView().findViewById(R.id.selectDate));
        PickDate = (ImageButton) (getView().findViewById(R.id.btncalender));

        /* get the current date */
        Date = Calendar.getInstance();

        /* add a click listener to the button   */
        PickDate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDateDialog(DateDisplay, Date);
            }
        });

        /* display the current date (this method is below)  */
        updateDisplay(DateDisplay, Date);

    }

    private DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            activeDate.set(Calendar.YEAR, year);
            activeDate.set(Calendar.MONTH, monthOfYear);
            activeDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
            updateDisplay(activeDateDisplay, activeDate);
            //unregisterDateDisplay();
        }
    };

    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DATE_DIALOG_ID:
                return new DatePickerDialog(getActivity(), dateSetListener, activeDate.get(Calendar.YEAR), activeDate.get(Calendar.MONTH), activeDate.get(Calendar.DAY_OF_MONTH));
        }
        return null;
    }


    protected void onPrepareDialog(int id, Dialog dialog) {
        switch (id) {
            case DATE_DIALOG_ID:
                ((DatePickerDialog) dialog).updateDate(activeDate.get(Calendar.YEAR), activeDate.get(Calendar.MONTH), activeDate.get(Calendar.DAY_OF_MONTH));
                break;
        }
    }

    private void showDateDialog(TextView dateDisplay, Calendar date) {
        activeDateDisplay = dateDisplay;
        activeDate = date;
        showDialog(DATE_DIALOG_ID);
    }

    private void showDialog(int dateDialogId) {


    }

    private void updateDisplay(TextView dateDisplay, Calendar date) {
        DateDisplay.setText(
                new StringBuilder()
                        .append(date.get(Calendar.DAY_OF_MONTH)).append("-")
                        .append(date.get(Calendar.MONTH) + 1).append("-")
                        .append(date.get(Calendar.YEAR)).append(" "));
    }

}

【问题讨论】:

    标签: android android-fragments datepicker onclick onclicklistener


    【解决方案1】:

    在您的按钮中单击像这样调用DateFragment

    yourButton.setOnClickListener(new View.OnClickListener() {
    
        @Override
        public void onClick(View v) {
    
            DialogFragment newFragment = new DateFragment();
            newFragment.show(getFragmentManager(), "DatePicker");
    
        }
    });
    

    对于日期片段

    public class DateFragment extends 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) {
            dob.setText(month+"/"+day+"/"+year);
            }
    
    }
    

    【讨论】:

    • 嗨@gargs 为什么我在“show(getFragmentManager(),..”上有错误?
    【解决方案2】:

    尝试这样的希望工作

    private void displayDate() {
    
        Calendar mCalendar = Calendar.getInstance();
        new DatePickerDialog(getActivity(), (OnDateSetListener) new OnDateSetListener() {
    
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
        int dayOfMonth) {
        // TODO Auto-generated method stub
           PickDate.append(monthOfYear + 1).append("-").append(
               .append("-").append(year));
    
        }
        }, mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH),
        mCalendar.get(Calendar.DATE));
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多