再次单击两次EditText时,不再显示DatePicker

时间:2015-03-30 11:52:59

标签: android

首先点击EditText,然后会显示DatePicker;但是在第二次点击时会显示virtualkeyboard!我不想要显示这个虚拟键盘!那么如何在点击DatePicker时始终显示EditText?以下是DatePicker的代码及其调用方式:

public class DatePickerUtil {

    private Context ctxt;
    private Calendar calendrier = Calendar.getInstance();
    private EditText chp;
    private OnFocusChangeListener focusChangeListener;
    private boolean canceled;
    private DatePickerDialog picker;

    public DatePickerUtil(Context context, EditText field) {

        ctxt = context;
        chp = field;
        canceled = false;

        focusChangeListener = new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    OnDateSetListener evt = new OnDateSetListener() {
                        @Override
                        public void onDateSet(DatePicker view, int annee, int mois, int jour) {
                            calendrier.set(annee, mois, jour);
                            if (!canceled)
                                chp.setText(DateFormat.format("yyyy-MM-dd", calendrier));
                        }
                    };
                    picker = new DatePickerDialog(ctxt, evt, calendrier.get(Calendar.YEAR),
                                                                    calendrier.get(Calendar.MONTH),
                                                                    calendrier.get(Calendar.DAY_OF_MONTH));
                    picker.setButton(DialogInterface.BUTTON_NEGATIVE, ctxt.getResources().getString(R.string.button_cancel), new OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                canceled = true;
                                dialog.dismiss();
                            }
                        });
                    picker.show();
                }
            }
        };
    }

    public void attache() {
        chp.setOnFocusChangeListener(focusChangeListener);
    }
}

在活动中:

EditText myDateField = findViewById(R.id.myDateField);
new DatePickerUtil(myActivity.this, myDateField).attache();

1 个答案:

答案 0 :(得分:1)

MotionEvent.ACTION_UP操作

中显示日期选择器
editText.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                   // write your datepicker displaying code here
                }
                return false;
            }
        });