android OnGlobalLayoutListener如何检索数据

时间:2012-11-16 15:48:00

标签: android listeners

我正在使用OnGlobalLayoutListener。如何使用此侦听器中的数据?特别是我需要lAngle。

        past_edittext.getViewTreeObserver().addOnGlobalLayoutListener(
            new OnGlobalLayoutListener() {

                @Override
                public void onGlobalLayout() {
                    int height = past_edittext.getHeight();
                    int width = past_edittext.getWidth();
                    int top = past_edittext.getTop();
                    int left = past_edittext.getLeft();

                    // center coordinates of EditText
                    past_edittextX = left + width / 2;
                    past_edittextY = top + height / 2;
                    lAngle = (float) (Math
                            .atan((totalCenterY - past_edittextY)
                                    / (totalCenterX - past_edittextX)) * 180 / Math.PI);
                }
            });

1 个答案:

答案 0 :(得分:1)

你班上的

声明如下

private float lAngle;

然后您可以在从globallayoutlistener

设置后访问lAngle
...
past_edittext.getViewTreeObserver().addOnGlobalLayoutListener(
        new OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                int height = past_edittext.getHeight();
                int width = past_edittext.getWidth();
                int top = past_edittext.getTop();
                int left = past_edittext.getLeft();

                // center coordinates of EditText
                past_edittextX = left + width / 2;
                past_edittextY = top + height / 2;
                lAngle = (float) (Math
                        .atan((totalCenterY - past_edittextY)
                                / (totalCenterX - past_edittextX)) * 180 / Math.PI);
            }
        });
}

public void someOtherMethod(){
    if (lAngle != null)
        // do something...