在我的应用程序中,我要求在同一个textView上使用单击和拖动事件。
我写了以下代码:
switch(event.getAction())
{
case MotionEvent.ACTION_UP:
// TextDialog.setVisibility(View.VISIBLE);
break;
case MotionEvent.ACTION_DOWN: {
disallowTouch(parent, true);
int downX = (int)event.getX();
int downY = (int)event.getY();
return false; // allow other events like Click to be processed
}
case MotionEvent.ACTION_MOVE:
int x = (int)event.getRawX();
int y= (int)event.getRawY();
layoutParams.leftMargin = x - 50;
layoutParams.topMargin = y - 70;
tvText.setLayoutParams(layoutParams);
break;
default:
break;
}
}
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextDialog.setVisibility(View.VISIBLE);
}
但只有Action_Move正在运行,onClick事件才会被解雇。
我只想在单击TextView时显示一个对话框。
我怎样才能做到这一点?
答案 0 :(得分:1)
同时使用setOnTouchListener和setOnClickListener
onTouch中的:
public boolean onTouch(View v, MotionEvent event) {
ViewParent parent = v.getParent();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
disallowTouch(parent, true);
downX = event.getX();
downY = event.getY();
return false; // allow other events like Click to be processed
}
}
private void disallowTouch(ViewParent parent, boolean isDisallow) {
if (parent != null) {
parent.requestDisallowInterceptTouchEvent(isDisallow);
}
}