图像按钮仅在单击两次时有效

时间:2017-07-15 09:32:40

标签: android onclick

详细信息:

我的活动中有一个EditText,右侧是可绘制的(日历图标)。如果我点击EditText或日历图标datepicker对话框应该打开。

问题:

如果我点击EditText datepicker对话框已正确打开,但如果我第一次点击Calendar图标,则不会发生任何事情。如果我再次单击相同的图标,则会打开datepicker对话框。

我已经在下面给出了右侧可绘制的ontouch:

 @Override
        public boolean onTouch(View v, MotionEvent event) {
            final int DRAWABLE_RIGHT = 2;


            if (event.getAction() == MotionEvent.ACTION_UP) {
                mValidationId = (Integer) v.getTag();

                if (mEditText.getCompoundDrawables()[DRAWABLE_RIGHT] != null
                        && !isErrorIconDisplayedForCalendarIcon) {
                    if (event.getRawX() >= (mEditText.getRight() - mEditText.getCompoundDrawables()[DRAWABLE_RIGHT]
                            .getBounds().width())) {
                        isErrorIconDisplayedForCalendarIcon = mValidationId == RegistrationValidator.VALIDATE_DATEOFBIRTH;
                        ViewUtils.hideVirturalKeyboard(mEditText);
                        mListener.OnErrorIconClick(mValidationId);
                        return true;
                    } else {
                        HideErrorIcon();
                        if (mValidationId == RegistrationValidator.VALIDATE_DATEOFBIRTH) {
                            mEditText.setCursorVisible(false);
                                mListener.OnCalendarIconClick();
                            ViewUtils.hideVirturalKeyboard(mEditText);
                        } else {
                            v.requestFocus();
                            ViewUtils.showVirturalKeyboard(mContext.getApplicationContext());

                            if (mValidationId == RegistrationValidator.VALIDATE_USERNAME ||
                                    mValidationId == RegistrationValidator.VALIDATE_PASSWORD) {
                                RegistrationUserCredentialsController controller = (
                                        (RegistrationUserCredentialsController) mDsmApplication
                                                .getController(RegistrationUserCredentialsController.TAG));
                                if (controller != null) {
                                    controller.OnRegistrationItemEditStart(mValidationId);
                                }
                            }
                        }
                    }
                } else {
                    isErrorIconDisplayedForCalendarIcon = false;
                    if (mValidationId == RegistrationValidator.VALIDATE_DATEOFBIRTH) {
                        mEditText.setCursorVisible(false);
                        mListener.OnCalendarIconClick();
                        ViewUtils.hideVirturalKeyboard(mEditText);
                    } else {
                        if (mCurrentEditText != null && !mEditText.equals(mCurrentEditText)) {
                            mListener.OnRegistrationItemEditCompleted(
                                    mCurrentEditText.getText().toString(), (Integer) mCurrentEditText.getTag()
                            );
                        }
                        mCurrentEditText = (CustomTextInputEditText) v;
                        v.requestFocus();
                        ViewUtils.showVirturalKeyboard(mContext.getApplicationContext());

                        if (mValidationId == RegistrationValidator.VALIDATE_USERNAME ||
                                mValidationId == RegistrationValidator.VALIDATE_PASSWORD) {
                            RegistrationUserCredentialsController controller = (
                                    (RegistrationUserCredentialsController) mDsmApplication
                                            .getController(RegistrationUserCredentialsController.TAG));
                            if (controller != null) {
                                controller.OnRegistrationItemEditStart(mValidationId);
                            }
                        }
                    }
                }

            }
            return false;
        }

在上面的代码中,我遇到了与出生日期相关的问题Edittext。如果我单击右侧可绘制的下划线颜色Edittext正在更改,则在第二次单击后,日期选择器对话框将打开。

2 个答案:

答案 0 :(得分:0)

  

尝试下面的内容可能会对你有所帮助。它对我有用。

mEditText.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        final int DRAWABLE_LEFT = 0;
        final int DRAWABLE_TOP = 1;
        final int DRAWABLE_RIGHT = 2;
        final int DRAWABLE_BOTTOM = 3;

        if(event.getAction() == MotionEvent.ACTION_UP) {
            if(event.getRawX() >= (mEditText.getRight() - mEditText.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
                // your action here

             return true;
            }
        }
        return false;
    }
});

答案 1 :(得分:-1)

在AndroidManifest.xml中为该活动添加此行

android:launchMode = "singleInstance"