如何在触摸内容时移动布局?

时间:2015-12-16 03:28:19

标签: android

我目前正在创建Android应用。我有一个登录活动,其中LinearLayout位于ImageViewGirdLayout位于EditTexts,其中LinearLayout很少。当任何EditTexts有焦点时,我想将AnimationListener移到指定的高度。我从这里尝试解决方案: Android translate animation - permanently move View to new position using AnimationListener并将onFocusChange放在<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:weightSum="6" android:background="#74B897" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:id="@+id/imageLayout" > <ImageView android:layout_width="247dp" android:layout_height="290dp" android:id="@+id/logo_imageView" android:layout_alignParentTop="true" android:layout_gravity="center_horizontal" android:layout_row="1" android:src="@drawable/white_logo" /> </LinearLayout> <GridLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:background="@drawable/layout_shape" android:layout_weight="2" android:layout_row="7" android:layout_column="1" android:weightSum="5" android:layout_below="@+id/logo_imageView" android:layout_marginLeft = "15dp" android:layout_marginRight = "15dp" android:layout_marginBottom = "30dp" > <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/fname_editText" android:hint="First Name" android:layout_rowWeight="0" android:layout_marginTop = "15dp" android:layout_row="1" android:layout_column="0" android:textColor="#FFFFFF" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/sname_editText" android:layout_below="@+id/fname_editText" android:hint="Second Name" android:layout_rowWeight="0" android:layout_row="2" android:layout_column="0" android:textColor="#FFFFFF"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/password_editText" android:inputType="textPassword" android:ems="10" android:hint="Password" android:layout_rowWeight="0" android:layout_row="3" android:layout_column="0" android:textColor="#FFFFFF"/> <Button android:layout_width="141dp" android:text="Log In" android:id="@+id/log_button" android:background="@drawable/button_shape" android:layout_marginTop="10dp" android:layout_marginBottom="40dp" android:layout_below="@+id/id_editText" android:layout_centerInParent="true" android:layout_gravity="center_horizontal" android:layout_rowWeight="1" android:textSize="20dp" android:textStyle="bold" android:layout_row="5" android:layout_column="0" android:layout_height="29dp" android:textColor="#636363" /> </GridLayout> </LinearLayout> 内,但就我而言,它似乎什么都不做。你能帮帮我吗?

XML:

EditText sname;
EditText password;
TextView info;
ImageView img;
EditText fname;
LinearLayout imageLayout;
int oldX, oldY, newX, newY;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login_layout);

    fname = (EditText) findViewById(R.id.fname_editText);
    sname = (EditText)findViewById(R.id.sname_editText);
    password = (EditText)findViewById(R.id.password_editText);
    img= (ImageView) findViewById(R.id.logo_imageView);
    imageLayout = (LinearLayout) findViewById(R.id.imageLayout);


    oldX = imageLayout.getWidth();
    oldY = imageLayout.getHeight();


    img.setImageResource(R.drawable.white_logo);
    Button log_Button = (Button) findViewById(R.id.log_button);

    fname.setFocusable(true);
    sname.setFocusable(true);
    password.setFocusable(true);

}



@Override
public void onFocusChange(View v, boolean hasFocus) {


    if(v.getId() == R.id.fname_editText || v.getId() == R.id.sname_editText || v.getId() == R.id.password_editText)
    {
        final ObjectAnimator moveDownAnim = ObjectAnimator.ofFloat(imageLayout, "translationY", 0.F, -300);
        final ObjectAnimator moveUpAnim = ObjectAnimator.ofFloat(imageLayout, "translationY", 300, 0.F);

        if(hasFocus){
            //first option
            moveDownAnim.start();

            /*
            second option
           TranslateAnimation anim = new TranslateAnimation(0, 0, 0, -300);

            anim.setFillAfter(false);
            anim.setDuration(1000);

            anim.setAnimationListener(new TranslateAnimation.AnimationListener() {

                @Override
                public void onAnimationStart(Animation animation) {
                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    imageLayout.clearAnimation();
                    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) imageLayout.getLayoutParams();
                    params.topMargin += -300;
                    imageLayout.setLayoutParams(params);

                    animation = new TranslateAnimation(0.0f, 0.0f, 0.0f, -300);
                    animation.setDuration(1);
                    imageLayout.startAnimation(animation);
                }
            });

            imageLayout.startAnimation(anim);*/
        }
    }
}

的活动:

public class LoginActivity extends Activity实现了View.OnFocusChangeListener {

CallbackObject

}

1 个答案:

答案 0 :(得分:0)

我没有看到你在代码中为EditTexts设置OnFocusChangeListener。

fname.setOnFocusChangeListener(this);
sname.setOnFocusChangeListener(this);