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

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
IFOUND解决方案,可绘制的端有一些我在运动动作中不包括触摸的位置,因此仅使用event.getx()
是不够的,您需要添加更多行.getPaddingright(
),您要做的就是用像这样的getright和getBound Line减去它: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: