android在屏幕上获取edittext的当前坐标

时间:2012-11-20 08:31:44

标签: android android-layout android-edittext

我需要在屏幕上获取edittext的当前坐标。我正在使用OnGlobalLayoutListener 但它被称为两次而不是一次,而另一个缺点就是它太晚了。 如果有任何方法如何在创建时立即获取editText的位置?

1 个答案:

答案 0 :(得分:2)

使用View.getLocationOnScreen()和/或getLocationInWindow()。 您可以在onWindowFocusChanged

中使用
@Override
    public void onWindowFocusChanged(boolean hasFocus) {
        // TODO Auto-generated method stub
        super.onWindowFocusChanged(hasFocus);

        int[] locationInWindow = new int[2];
        object.getLocationInWindow(locationInWindow);

        Log.v("TAG","getLocationInWindow() - "+ locationInWindow[0] + " : " + locationInWindow[1]);
    }
相关问题