使用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) {
- // TODO Auto-generated method stub
- 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();
- }
- });
时间: 2024-11-05 11:51:26