Detect touch position in Android
本问题已经有最佳答案,请猛点这里访问。
任何人都可以告诉我如何在Android中检测并获得触摸位置。
假设,如果我触摸屏幕,那么我应该在标签中获得触摸位置。
如果使用PhoneGap
你可以使用HTML / JS。
请参阅下文
推荐这个链接 - http://miloq.blogspot.in/2011/05/coordinates-mouse-click-canvas.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | <style type="text/css"> #canvas{background-color: #000;} </style> <script type="text/javascript"> document.addEventListener("DOMContentLoaded", init, false); function init() { var canvas = document.getElementById("canvas"); canvas.addEventListener("mousedown", getPosition, false); } function getPosition(event) { var x = new Number(); var y = new Number(); var canvas = document.getElementById("canvas"); if (event.x != undefined && event.y != undefined) { x = event.x; y = event.y; } else // Firefox method to get the position { x = event.clientX + document.body.scrollLeft + document.documentElement.scrollLeft; y = event.clientY + document.body.scrollTop + document.documentElement.scrollTop; } x -= canvas.offsetLeft; y -= canvas.offsetTop; alert("x:" + x +" y:" + y); } |
试试这个,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final TextView textView = (TextView)findViewById(R.id.textView); // this is the view on which you will listen for touch events final View touchView = findViewById(R.id.touchView); touchView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { textView.setText("Touch coordinates :" + String.valueOf(event.getX()) +"x" + String.valueOf(event.getY())); return true; } }); } |