如何在选择任何EditText时执行操作?

时间:2015-12-16 11:55:26

标签: android android-edittext

我正在写一个关于android的应用程序,我有一个登录活动。因此,当有人选择EditTexts中的任何一个时,布局会在给定高度上升,否则布局会转到原始位置;所有动画都是这样创建的

public void onFocusChange(View v, boolean hasFocus) {

    if(v.getId() == R.id.editText1 || v.getId() == R.id.editText2 || v.getId() == R.id.editText3) {
        final ObjectAnimator moveDownAnim = ObjectAnimator.ofFloat(imageLayout, "translationY", 0.F, -370);
        final ObjectAnimator moveUpAnim = ObjectAnimator.ofFloat(imageLayout, "translationY", 370, 0.F);

        if (hasFocus) {
            moveDownAnim.start();
        } else {
            moveUpAnim.start();
        }
    }
}

当有人选择例如editText1这一切都在上升,但当用户从editText1直接转到editText2时,首先是布局向下然后再向上。有没有办法避免它并保持布局,直到用户点击非EditText的东西?

2 个答案:

答案 0 :(得分:2)

您可以使用v.getClass().getSimpleName()获取班级名称。如果ViewEditText,则会返回值为EditText的字符串。 您可以像这样更改代码:

    public void onFocusChange(View v, boolean hasFocus) {

if(v.getClass().getSimpleName().equals("EditText")) {
    if(v.getId() == R.id.editText1 || v.getId() == R.id.editText2 || v.getId() == R.id.editText3) {
        final ObjectAnimator moveDownAnim = ObjectAnimator.ofFloat(imageLayout, "translationY", 0.F, -370);
        final ObjectAnimator moveUpAnim = ObjectAnimator.ofFloat(imageLayout, "translationY", 370, 0.F);

        if (hasFocus) {
            moveDownAnim.start();
        } else {
            moveUpAnim.start();
        }
    }
}
else
{
/// NOT EditText, do whatever you want here
}
}

答案 1 :(得分:0)

我解决了。这有点解决方案,但仍然有效:)

@Override
public boolean onTouch(View v, MotionEvent event) {

    final ObjectAnimator moveDownAnim = ObjectAnimator.ofFloat(gridLayout, "translationY", 0.F, -370);
    final ObjectAnimator moveUpAnim = ObjectAnimator.ofFloat(gridLayout, "translationY", 370, 0.F);

     if(v.getId() == R.id.editText1 || v.getId() == R.id.editText2 || v.getId() == R.id.editText3) {
    {
        if(up == false) {
            moveDownAnim.start();
        }
    }
    else if (!(v.getId() == R.id.editText1 || v.getId() == R.id.editText2 || v.getId() == R.id.editText3)){}
    else{
        moveUpAnim.start();
        up = false;
    }

    return false;
}

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

    if(v.getId() == R.id.editText1 || v.getId() == R.id.editText2 || v.getId() == R.id.editText3){
        if(hasFocus){
            up = true;
        }
    }
}