0

我正在使用 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);
                }
            });
4

1 回答 1

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...
于 2012-12-27T21:28:20.540 回答