在同一视图上实现拖动和双击 android
implement drag and double tap on the same view android
我想在 EditText
上实现拖动和双击事件,但我只能实现两者中的一个。这背后的想法是,当用户双击编辑文本时,编辑文本将变为启用状态,并且通过拖动,他可以将其拖动到他想要的任何位置。
text.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(gestureDetector.onTouchEvent(event)==true && gestureDetector!=null){
return gestureDetector.onTouchEvent(event);
}
else
{drag(event, v);
return true; }
}
private void drag(MotionEvent event, View v) {
// TODO Auto-generated method stub
RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams) v
.getLayoutParams();
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE: {
params.topMargin = (int) event.getRawY() - (v.getHeight());
params.leftMargin = (int) event.getRawX()
- (v.getWidth() / 2);
v.setLayoutParams(params);
break;
}
case MotionEvent.ACTION_UP: {
params.topMargin = (int) event.getRawY() - (v.getHeight());
params.leftMargin = (int) event.getRawX()
- (v.getWidth() / 2);
v.setLayoutParams(params);
break;
}
case MotionEvent.ACTION_DOWN: {
v.setLayoutParams(params);
break;
}
}
}
});
我找到了解决方案
// TODO Auto-generated method stub
int X = (int) event.getRawX();
int Y = (int) event.getRawY();
layoutParams = (FrameLayout.LayoutParams) v.getLayoutParams();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
FrameLayout.LayoutParams lParams = (FrameLayout.LayoutParams) v
.getLayoutParams();
_xDelta = X - lParams.leftMargin;
_yDelta = Y - lParams.topMargin;
startTime = System.currentTimeMillis();
if (upCounter != 0) {
timeBetweenTwoClick = startTime - endTime;
}
mSingleStart = System.currentTimeMillis();
break;
case MotionEvent.ACTION_UP:
endTime = System.currentTimeMillis();
mSingleEnd = System.currentTimeMillis();
currentClickTime = endTime - startTime;
consecutiveTwoClickTime = currentClickTime
+ previousClickTime;
// textview single tap detection
if (mSingleEnd - mSingleStart <= mSingleTimeDiffrence) {
Toast.makeText(MainActivity.this, "tap", 1500).show();
if (mSingleCount == false) {
GradientDrawable gd = new GradientDrawable();
gd.setCornerRadius(5);
gd.setStroke(3, 0xFF000000);
text.setBackgroundDrawable(gd);
verticalSeekBar.setVisibility(View.VISIBLE);
verticalSeekBar
.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(
SeekBar seekBar) {
// TODO Auto-generated method
// stub
}
@Override
public void onStartTrackingTouch(
SeekBar seekBar) {
// TODO Auto-generated method
// stub
}
@Override
public void onProgressChanged(
SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method
// stub
text.setTextSize(progress + 20);
text2.setTextSize(progress + 20);
}
});
mSingleCount = true;
} else {
text.setBackgroundDrawable(null);
mSingleCount = false;
verticalSeekBar.setVisibility(View.INVISIBLE);
}
}
if (upCounter == 0) {
++upCounter;
previousClickTime = currentClickTime;
}
else {
previousClickTime = currentClickTime;
if ((consecutiveTwoClickTime + timeBetweenTwoClick) <= doubleClickTimeDiffrence) {
if (layoutParams != null)
text2.setLayoutParams(layoutParams);
text2.setVisibility(View.VISIBLE);
text2.requestFocus();
if (text2.getText().toString()
.equalsIgnoreCase("enter by double tap")) {
text2.setText("");
}
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(text2,
InputMethodManager.SHOW_IMPLICIT);
text.setVisibility(View.INVISIBLE);
Toast.makeText(MainActivity.this, "double tap",
1500).show();
}
}
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
case MotionEvent.ACTION_MOVE:
layoutParams.leftMargin = X - _xDelta;
layoutParams.topMargin = Y - _yDelta;
if (layoutParams.leftMargin < 0) {
layoutParams.leftMargin = 20;
} else if (layoutParams.leftMargin > mainimage.getWidth()) {
layoutParams.leftMargin = mainimage.getWidth() - 20;
}
if (layoutParams.topMargin < 0) {
layoutParams.topMargin = 20;
} else if (layoutParams.topMargin > mainimage.getHeight() - 100) {
text.setText(text2.getText().toString());
layoutParams.topMargin = mainimage.getHeight() - 90
- text.getHeight();
// layoutParams.leftMargin = 20;
}
text.setX(layoutParams.leftMargin);
text.setY(layoutParams.topMargin);
v.setLayoutParams(text.getLayoutParams());
Log.i("mainimage.getHeight() ",
(mainimage.getHeight() - 60) + " "
+ layoutParams.topMargin + " "
+ text.getHeight());
// if (text.getLeft() >= 0 && text.getTop() >= 0
// && text.getRight() < width
// && text.getBottom() < mainimage.getHeight()) {
//
// text.setX(layoutParams.leftMargin);
// text.setY(layoutParams.topMargin);
// } else if (text.getTop() < 0) {
//
// text.setX(layoutParams.leftMargin);
// text.setY(10);
// } else if (text.getBottom() > mainimage.getHeight()) {
// text.setX(layoutParams.leftMargin);
// text.setY(mainimage.getHeight() - 50);
// } else if (text.getLeft() < 0) {
// text.setX(10);
// text.setY(layoutParams.topMargin);
// } else if (text.getRight() > mainimage.getWidth()) {
//
// text.setX(mainimage.getWidth() - 10);
// text.setY(layoutParams.topMargin);
// }
// else {
// text.setX(20);
// text.setY(20);
// }
// layoutParams.rightMargin = -250;
// layoutParams.bottomMargin = -250;
// if (text.getWidth() < (width - 50)) {
//
// }
// text.setWidth(width - text.getLeft());
// Log.i("From Current position: ",
// "Left Margin: " + X + " " + _xDelta
// + "Left Margin: " + Y + " " + _yDelta
// + "layoutParams.leftMargin: "
// + layoutParams.leftMargin
// + " layoutParams.topMargin: "
// + layoutParams.topMargin
// + " layoutParams.height - width: "
// + layoutParams.height + " " + width
// + " " + (width - text.getWidth()) + " "
// + text.getWidth());
break;
我想在 EditText
上实现拖动和双击事件,但我只能实现两者中的一个。这背后的想法是,当用户双击编辑文本时,编辑文本将变为启用状态,并且通过拖动,他可以将其拖动到他想要的任何位置。
text.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(gestureDetector.onTouchEvent(event)==true && gestureDetector!=null){
return gestureDetector.onTouchEvent(event);
}
else
{drag(event, v);
return true; }
}
private void drag(MotionEvent event, View v) {
// TODO Auto-generated method stub
RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams) v
.getLayoutParams();
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE: {
params.topMargin = (int) event.getRawY() - (v.getHeight());
params.leftMargin = (int) event.getRawX()
- (v.getWidth() / 2);
v.setLayoutParams(params);
break;
}
case MotionEvent.ACTION_UP: {
params.topMargin = (int) event.getRawY() - (v.getHeight());
params.leftMargin = (int) event.getRawX()
- (v.getWidth() / 2);
v.setLayoutParams(params);
break;
}
case MotionEvent.ACTION_DOWN: {
v.setLayoutParams(params);
break;
}
}
}
});
我找到了解决方案
// TODO Auto-generated method stub
int X = (int) event.getRawX();
int Y = (int) event.getRawY();
layoutParams = (FrameLayout.LayoutParams) v.getLayoutParams();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
FrameLayout.LayoutParams lParams = (FrameLayout.LayoutParams) v
.getLayoutParams();
_xDelta = X - lParams.leftMargin;
_yDelta = Y - lParams.topMargin;
startTime = System.currentTimeMillis();
if (upCounter != 0) {
timeBetweenTwoClick = startTime - endTime;
}
mSingleStart = System.currentTimeMillis();
break;
case MotionEvent.ACTION_UP:
endTime = System.currentTimeMillis();
mSingleEnd = System.currentTimeMillis();
currentClickTime = endTime - startTime;
consecutiveTwoClickTime = currentClickTime
+ previousClickTime;
// textview single tap detection
if (mSingleEnd - mSingleStart <= mSingleTimeDiffrence) {
Toast.makeText(MainActivity.this, "tap", 1500).show();
if (mSingleCount == false) {
GradientDrawable gd = new GradientDrawable();
gd.setCornerRadius(5);
gd.setStroke(3, 0xFF000000);
text.setBackgroundDrawable(gd);
verticalSeekBar.setVisibility(View.VISIBLE);
verticalSeekBar
.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(
SeekBar seekBar) {
// TODO Auto-generated method
// stub
}
@Override
public void onStartTrackingTouch(
SeekBar seekBar) {
// TODO Auto-generated method
// stub
}
@Override
public void onProgressChanged(
SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method
// stub
text.setTextSize(progress + 20);
text2.setTextSize(progress + 20);
}
});
mSingleCount = true;
} else {
text.setBackgroundDrawable(null);
mSingleCount = false;
verticalSeekBar.setVisibility(View.INVISIBLE);
}
}
if (upCounter == 0) {
++upCounter;
previousClickTime = currentClickTime;
}
else {
previousClickTime = currentClickTime;
if ((consecutiveTwoClickTime + timeBetweenTwoClick) <= doubleClickTimeDiffrence) {
if (layoutParams != null)
text2.setLayoutParams(layoutParams);
text2.setVisibility(View.VISIBLE);
text2.requestFocus();
if (text2.getText().toString()
.equalsIgnoreCase("enter by double tap")) {
text2.setText("");
}
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(text2,
InputMethodManager.SHOW_IMPLICIT);
text.setVisibility(View.INVISIBLE);
Toast.makeText(MainActivity.this, "double tap",
1500).show();
}
}
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
case MotionEvent.ACTION_MOVE:
layoutParams.leftMargin = X - _xDelta;
layoutParams.topMargin = Y - _yDelta;
if (layoutParams.leftMargin < 0) {
layoutParams.leftMargin = 20;
} else if (layoutParams.leftMargin > mainimage.getWidth()) {
layoutParams.leftMargin = mainimage.getWidth() - 20;
}
if (layoutParams.topMargin < 0) {
layoutParams.topMargin = 20;
} else if (layoutParams.topMargin > mainimage.getHeight() - 100) {
text.setText(text2.getText().toString());
layoutParams.topMargin = mainimage.getHeight() - 90
- text.getHeight();
// layoutParams.leftMargin = 20;
}
text.setX(layoutParams.leftMargin);
text.setY(layoutParams.topMargin);
v.setLayoutParams(text.getLayoutParams());
Log.i("mainimage.getHeight() ",
(mainimage.getHeight() - 60) + " "
+ layoutParams.topMargin + " "
+ text.getHeight());
// if (text.getLeft() >= 0 && text.getTop() >= 0
// && text.getRight() < width
// && text.getBottom() < mainimage.getHeight()) {
//
// text.setX(layoutParams.leftMargin);
// text.setY(layoutParams.topMargin);
// } else if (text.getTop() < 0) {
//
// text.setX(layoutParams.leftMargin);
// text.setY(10);
// } else if (text.getBottom() > mainimage.getHeight()) {
// text.setX(layoutParams.leftMargin);
// text.setY(mainimage.getHeight() - 50);
// } else if (text.getLeft() < 0) {
// text.setX(10);
// text.setY(layoutParams.topMargin);
// } else if (text.getRight() > mainimage.getWidth()) {
//
// text.setX(mainimage.getWidth() - 10);
// text.setY(layoutParams.topMargin);
// }
// else {
// text.setX(20);
// text.setY(20);
// }
// layoutParams.rightMargin = -250;
// layoutParams.bottomMargin = -250;
// if (text.getWidth() < (width - 50)) {
//
// }
// text.setWidth(width - text.getLeft());
// Log.i("From Current position: ",
// "Left Margin: " + X + " " + _xDelta
// + "Left Margin: " + Y + " " + _yDelta
// + "layoutParams.leftMargin: "
// + layoutParams.leftMargin
// + " layoutParams.topMargin: "
// + layoutParams.topMargin
// + " layoutParams.height - width: "
// + layoutParams.height + " " + width
// + " " + (width - text.getWidth()) + " "
// + text.getWidth());
break;