简体   繁体   中英

Get X and Y coordinates of OnLongClickListener

I want to get the X & Y coordinates of the location where i long click and set the button to this location, but i don't get it because there is no MotionEvent as with the onClick method.

    private View.OnLongClickListener layoutOnTouchListener(){
    return new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            RelativeLayout.LayoutParams positionRules = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            positionRules.leftMargin = (int) v.getX();
            positionRules.topMargin = (int) v.getY();
            mainButton.setLayoutParams(positionRules);
            Log.d("X", String.valueOf(v.getX()));
            return true;
        }
    };
}

Thats the code i tried.

You can try setonkeyListener and check whether it's long click or not

@Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
    if (null != keyEvent && keyEvent.getAction() == KeyEvent.ACTION_DOWN) {

        if (System.currentTimeMillis() - mLastClickedTime > DEFAULT_PRESSED_DELAY) {
            Log.d(TAG, "onKey: single  presed");
            onKeyPressed(view, keyEvent);

        } else {
            Log.d(TAG, "onKey: repeat pressed");
            onKeyRepeatPressed(view);

        }
        mLastClickedTime=System.currentTimeMillis();

    }
    return false;
}

If it's long click take the x and y points

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM