触摸侦听器无法在编辑文本上工作

发布于 2025-02-02 23:52:34 字数 1051 浏览 1 评论 0原文

我正在尝试在编辑文本上使用可绘制的权利,以便在对话框的帮助下用户一些信息。现在我一直面临的是触摸侦听器在肖像模式下的完美工作,但是当我切换到景观模式并单击编辑文本时,对话框不断显示,无论我是否单击可绘制的图标,我也都做了单独的布局景观模式的文件夹,我在其中添加了布局文件。代码段附在我正在使用的下面:

et_cnicPassport.setOnTouchListener(new View.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() >= (et_cnicPassport.getRight() - et_cnicPassport.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {

                        DialogHandler.errorDialog(ForgotPasswordActivity.this, "Forgot Password",
                                "CNIC (xxxxx-xxxxxxx-x)" + "\n" + "Passport No.");
                        return true;
                    }
                }
                return false;
            }
        });

I'm trying to use drawable right on edit text in order to user some information with the help of dialog box. now the that I have been facing is that touch listener works perfect in portrait mode but when I switch to landscape mode and click on edit text the dialog kept on showing no matter if I click on drawable icon or not, I have also made separate layout folder for landscape mode and I have the layout file added in it. The code snippet is attached below that I'm using:

et_cnicPassport.setOnTouchListener(new View.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() >= (et_cnicPassport.getRight() - et_cnicPassport.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {

                        DialogHandler.errorDialog(ForgotPasswordActivity.this, "Forgot Password",
                                "CNIC (xxxxx-xxxxxxx-x)" + "\n" + "Passport No.");
                        return true;
                    }
                }
                return false;
            }
        });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听不够的曲调 2025-02-09 23:52:34

IFOUND解决方案,可绘制的端有一些我在运动动作中不包括触摸的位置,因此仅使用event.getx()是不够的,您需要添加更多行.getPaddingright(),您要做的就是用像这样的getright和getBound Line减去它:

if (event.getX() >= (yourEditText.getWidth() - yourEditText.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width() - yourEditText.getPaddingRight()))

Ifound solution to it, the drawable end had some padding that I wasn't including in the motion action to detect the place for touch so using only event.getX()is not enough you need to add one more line i.e. yourEditText.getPaddingRight() and all you have to do is subtract it with getright and getbound line like this:

if (event.getX() >= (yourEditText.getWidth() - yourEditText.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width() - yourEditText.getPaddingRight()))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文