class="FocusMe">使用view中的getLocationOnScreen方法,即可:
[java] view plaincopy
- final int[] locations = new int[2];
- Button btn = (Button) findViewById(R.id.btn);<span style="font-family: Arial, Helvetica, sans-serif;"> </span>
[java] view plaincopy
- btn.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View v) {
-
- v.getLocationOnScreen(locations);
- Toast.makeText(MainActivity.this, "x is: " + locations[0], Toast.LENGTH_SHORT).show();
- Toast.makeText(MainActivity.this, "y is: " + locations[1], Toast.LENGTH_SHORT).show();
- }
- });