EditText上的DatePickerDialog单击事件(运行问题)

时间:2015-12-30 21:12:27

标签: android datepicker

我正在实现一个片段,我有一个edittext,我想点击它时弹出一个日期选择器 我使用了以下链接

Datepicker: How to popup datepicker when click on edittext

但改变以下内容: 我已经添加了: private static final String TAG =" AppoinFragment&#34 ;; 和 字符串myFormat =" MM / dd / yy&#34 ;;   SimpleDateFormat sdf = new SimpleDateFormat(myFormat,Locale.US);

并替换nextField.requestFocus(); with fullname.requestFocus(); 运行我的应用程序并单击edittext时没有任何反应。 这是我使用的代码:

AppointFragment.java

package com.example.appointapp;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.EditText;

public class AppointFragment extends Fragment{

    private static final String TAG = "AppoinFragment";
    EditText fullname;

    private Appointment mappoitnemnt;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mappoitnemnt= new Appointment();


}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
    View v = inflater.inflate(R.layout.fragment_appoint, parent, false);



    fullname = (EditText)v.findViewById(R.id.datea_text);

    fullname.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                DialogFragment datePickerFragment = new DatePickerFragment() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int day) {
                        Log.d(TAG, "onDateSet");
                        Calendar c = Calendar.getInstance();
                        c.set(year, month, day);

                        String myFormat = "MM/dd/yy"; 
                        SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

                        fullname.setText(sdf.format(c.getTime()));

                        fullname.requestFocus(); 
                    }
                };
                datePickerFragment.show(getActivity().getSupportFragmentManager(), "datePicker");
            }
        }
    });


    return v;
}
}

DataPickerFragment.java

package com.example.appointapp;

import java.util.Calendar;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.DatePicker;

public  class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        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);

        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    @Override
    public void onDateSet(DatePicker view, int year, int month, int day) {

    }
}

请帮我解决问题

0 个答案:

没有答案