0

我需要一个带有三个图像按钮的 EditText,我设法通过使用相对布局并将图像按钮放置在 edittext 上来创建它。就像是: 布局

我试图在 EditText 聚焦时为按钮 IB1 和 IB2 的交换设置动画,并在失去焦点时再次将它们交换回来。我也设法做到了这一点,但是当我尝试更新按钮 LayoutParams onAnimationEnd 时出现问题(否则像素会更改屏幕上的位置,但底层视图仍处于其原始位置)。

问题:当我更新似乎再次触发 EditText 焦点事件的 ImageButton layoutParams 时 - 一旦您点击 edittext 小部件,按钮就会开始永久地来回移动。

相关的 XML:

<RelativeLayout
   android:id="@+id/commentView" 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content">
   <EditText
   android:id="@+id/commentBox"
   android:maxLines="1"
   android:scrollHorizontally="true"
   android:paddingTop="10dp"
   android:paddingBottom="10dp"
   android:inputType="text" 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:imeOptions="actionDone"
   android:paddingRight="100dp"
   android:paddingLeft="55dp"
   android:background="@drawable/comment"
   android:layout_marginLeft="2dp"
   android:layout_marginRight="2dp"
   android:hint="Your comment..."
   />
   <ImageButton
    android:id="@+id/share"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/commentBox"
    android:layout_marginRight="2dp"
    android:src="@drawable/sharebutton"
    android:background="@null"
    android:layout_centerVertical="true" />
   <ImageButton 
    android:id="@+id/like"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/commentBox"
    android:src="@drawable/likebutton"
    android:background="@null"
    android:layout_marginLeft="4dp"
    android:layout_centerVertical="true" />
   <ImageButton 
    android:id="@+id/commentimagebutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@+id/share"
    android:src="@drawable/commentbutton"
    android:background="@null"
    android:layout_marginRight="4dp"
    android:layout_centerVertical="true" />

以及相关的代码片段:

commentBox.setOnFocusChangeListener(new OnFocusChangeListener() {

                @Override
                public void onFocusChange(View view, boolean hasFocus) {
                    RelativeLayout parent = (RelativeLayout) view.getParent();
                    ImageButton commentView = (ImageButton) parent.findViewById(R.id.commentimagebutton);
                    ImageButton likeView = (ImageButton) parent.findViewById(R.id.like);
                    if (hasFocus) {
                        swapViews(likeView, commentView);
                    }
                }
            }); 

和:

private void swapViews(final ImageButton firstView, final ImageButton secondView)
{
    //get view positions and calculate distance
    int secondPosition[] = new int[2];
    int firstPosition[] = new int[2];
    secondView.getLocationOnScreen(secondPosition);
    firstView.getLocationOnScreen(firstPosition);
    int moveDistance = Math.abs(secondPosition[0]-firstPosition[0]);

    //get LayoutParams
    final RelativeLayout.LayoutParams firstViewLayout = (RelativeLayout.LayoutParams) firstView.getLayoutParams();
    final RelativeLayout.LayoutParams secondViewLayout = (RelativeLayout.LayoutParams) secondView.getLayoutParams();

    //set up animations
    TranslateAnimation moveLeft = new TranslateAnimation( 0, -moveDistance, 0, 0);
    moveLeft.setDuration(1000);
    moveLeft.setFillAfter(true);

    TranslateAnimation moveRight = new TranslateAnimation( 0, moveDistance, 0, 0);
    moveRight.setDuration(1000);
    moveRight.setFillAfter(true);

    if (secondPosition[0] > firstPosition[0]) {
        moveLeft.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {}

            @Override
            public void onAnimationRepeat(Animation animation) {}

            @Override
            public void onAnimationEnd(Animation animation) {
                //change positions
                secondView.clearAnimation();
                secondViewLayout.addRule(RelativeLayout.ALIGN_LEFT, R.id.commentBox);
                secondViewLayout.addRule(RelativeLayout.LEFT_OF, 0);
                secondView.setLayoutParams(secondViewLayout);
            }
        });

        moveRight.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {}

            @Override
            public void onAnimationRepeat(Animation animation) {}

            @Override
            public void onAnimationEnd(Animation animation) {
                //change positions
                firstView.clearAnimation();
                firstViewLayout.addRule(RelativeLayout.ALIGN_LEFT, 0);
                firstViewLayout.addRule(RelativeLayout.LEFT_OF, R.id.share);
                firstView.setLayoutParams(firstViewLayout);
            }
        });

        //animate
        secondView.startAnimation(moveLeft);
        firstView.startAnimation(moveRight);
    } else {
        moveLeft.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {}

            @Override
            public void onAnimationRepeat(Animation animation) {}

            @Override
            public void onAnimationEnd(Animation animation) {
                //change positions
                firstView.clearAnimation();
                firstViewLayout.addRule(RelativeLayout.ALIGN_LEFT, R.id.commentBox);
                firstViewLayout.addRule(RelativeLayout.LEFT_OF, 0);
                firstView.setLayoutParams(firstViewLayout);
            }
        });

        moveRight.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {}

            @Override
            public void onAnimationRepeat(Animation animation) {}

            @Override
            public void onAnimationEnd(Animation animation) {
                //change positions
                secondView.clearAnimation();
                secondViewLayout.addRule(RelativeLayout.ALIGN_LEFT, 0);
                secondViewLayout.addRule(RelativeLayout.LEFT_OF, R.id.share);
                secondView.setLayoutParams(secondViewLayout);
            }
        });

        //animate
        secondView.startAnimation(moveRight);
        firstView.startAnimation(moveLeft);
    }
}

注意:我使用的是 API 级别 10。我也尝试在 EditText 上使用 onClickListener,但是 1. 第一次点击 edittext 时会聚焦它,只有第二次点击会被检测为点击 2. 它不允许我交换一旦edittext失去焦点,按钮就会返回。

谢谢!

4

1 回答 1

0

首先,向 中添加其他规则LayoutParams不会删除当前规则,因此您只剩下:

<ImageButton
...
    android:layout_alignRight="@+id/commentBox"
    android:layout_alignLeft="@+id/commentBox"
...
 />

LayoutParams.removeRule()通过(添加)删除旧规则API 17,或者尝试创建一个新规则LayoutParams,看看是否有效。

其次,您可能希望view.requestLayout();在更改其LayoutParams.

于 2013-11-14T13:26:31.350 回答