返回Android对话框视图?

时间:2014-01-12 06:47:54

标签: android dialog

我正在使用此代码创建自定义对话框。这很好,但出于某种原因,我想返回或获取对话框视图。这就是原因:我希望对对话框设置动画以在对话框中实现展开/折叠功能,为此我需要拥有对话框的视图。

                final Dialog dialog = new Dialog(getActivity(),
                    R.style.CustomDialog);
            dialog.setContentView(R.layout.dialog_enter_name);


            dialog.setTitle("Hello");


            Button dialogButtonOK = (Button) dialog
                    .findViewById(R.id.dialogButtonOK);
            Button dialogButtonCancel = (Button) dialog
                    .findViewById(R.id.dialogButtonCancel);
            nameField = (EditText) dialog.findViewById(R.id.nameField);
            lastNameField = (EditText) dialog
                    .findViewById(R.id.familyField);

            //And some code for OK/Cancel button.

这是我将用于为对话框设置动画的类。如您所见,它需要一个视图。

    public class HeightAnimation extends Animation {
    protected final int originalHeight;
    protected final View view;
    protected float perValue;

    public HeightAnimation(View view, int fromHeight, int toHeight) {
        this.view = view;
        this.originalHeight = fromHeight;
        this.perValue = (toHeight - fromHeight);
    }

    @Override
    protected void applyTransformation(float interpolatedTime,
            Transformation t) {
        view.getLayoutParams().height = (int) (originalHeight + perValue
                * interpolatedTime);
        view.requestLayout();
    }

    @Override
    public boolean willChangeBounds() {
        return true;
    }
}

这就是我如何使用上面的课程:

                HeightAnimation heightAnim = new HeightAnimation(dialogview, 100, 400);
            heightAnim.setDuration(1000);
            dialogview.startAnimation(heightAnim);

我上面使用的对话框视图应该是Dialog的视图。但我不知道如何做到这一点?

编辑:我刚刚发现了如何做到这一点:

            final Dialog dialog = new Dialog(getActivity(),
                    R.style.CustomDialog);
            LayoutInflater factory = LayoutInflater.from(getActivity());
            final View dialogview = factory.inflate(
                    R.layout.dialog_enter_name, null);


            dialog.setContentView(dialogview);

0 个答案:

没有答案