簡體   English   中英

獲取OnLongClickListener的X和Y坐標

[英]Get X and Y coordinates of OnLongClickListener

我想獲取我長按的位置的X和Y坐標並將按鈕設置到該位置,但是我沒有得到它,因為沒有像onClick方法那樣的MotionEvent。

    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;
        }
    };
}

那就是我嘗試過的代碼。

您可以嘗試setonkeyListener並檢查是否長按

@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;
}

如果長按,則取x和y點

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM