我有一个可以在视图上拖动的TextView。它也应该是可单击的以显示对话框并修改textview上的文本。我的问题是onClick事件和onTouch监听器上的ACTION_UP根本没有被触发..只要单击/触摸文本视图就会触发onDrag的ACTION_UP,这使得很难拖动文本视图。
myTextView.setOnTouchListener(this);
myTextView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
displayDialog(caption);
}
});
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
// TODO Auto-generated method stub
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN: {
ClipData data = ClipData.newPlainText("", "");
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(data, shadowBuilder, view, 0);
view.setVisibility(View.VISIBLE);
break;
}
}
return true;
}
@Override
public boolean onDrag(View v, DragEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
case DragEvent.ACTION_DROP: {
this.myTextView.setX(event.getX()
- (this.myTextView.getWidth() / 2));
this.myTextView.setY(event.getY()
- (this.myTextView.getHeight() / 2));
break;
}
**case DragEvent.ACTION_DRAG_ENDED:** {
displayDialog(this.myTextView.getText().toString());
break;
}
}
return true;
}
public void displayDialog(String caption) {
final EditText myEditText = new EditText(this.getActivity());
if (caption != null && !caption.isEmpty()) {
myEditText.setText(caption);
}
AlertDialog.Builder dialog = new AlertDialog.Builder(this.getActivity())
.setView(myEditText)
dialog.show();
}
更新:抱歉..明白了..我刚搞砸了我的拖拽事件..用答案修改了问题