2011-12-07 26 views
2

我有一个Android视图,其中包含一个RadioGroup和三个RadioButton。当选中某个RadioButton时,用户还必须将文本输入到EditText控件中。如果选择其他两个RadioButton中的任何一个,则不需要此额外信息。我可以用动画隐藏Android控件吗?

我正在使用RadioGroup的OnCheckedChangedListener来确定何时检查新的RadioButton,并通过将EditText的可见性设置为View.GONE来隐藏EditText。然而,这有点刺耳,我想知道是否有一种方法可以根本改变过渡。这是可能的,如果是的话,什么是入门的关键?

回答

1

我已经想出以下可行的解决方案,这是基于代码我在http://tech.chitgoks.com/2011/10/29/android-animation-to-expand-collapse-view-its-children/

发现在我的活动:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.edit_account); 
    companyGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    { 
     @Override 
     public void onCheckedChanged(RadioGroup group, int checkedId) 
     { 
      if (checkedId == R.id.companyRadio) 
       EDNUtils.expandCollapse(companyNameText, true, 500); 
      else 
       EDNUtils.expandCollapse(companyNameText, false, 500); 
     } 
    }); 
} 

实施来自EDNUtils:

public static Animation expandCollapse(final View v, final boolean expand) 
{  
    return expandCollapse(v, expand, 1000); 
} 

public static Animation expandCollapse(final View v, final boolean expand, final int duration) 
{ 
    int currentHeight = v.getLayoutParams().height; 
    v.measure(MeasureSpec.makeMeasureSpec(((View)v.getParent()).getMeasuredWidth(), MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); 
    final int initialHeight = v.getMeasuredHeight(); 

    if ((expand && currentHeight == initialHeight) || (!expand && currentHeight == 0)) 
     return null; 

    if (expand) 
     v.getLayoutParams().height = 0; 
    else 
     v.getLayoutParams().height = initialHeight; 
    v.setVisibility(View.VISIBLE); 

    Animation a = new Animation() 
    { 
     @Override 
     protected void applyTransformation(float interpolatedTime, Transformation t) 
     { 
      int newHeight = 0; 
      if (expand) 
       newHeight = (int) (initialHeight * interpolatedTime); 
      else 
       newHeight = (int) (initialHeight * (1 - interpolatedTime)); 
      v.getLayoutParams().height = newHeight;    
      v.requestLayout(); 

      if (interpolatedTime == 1 && !expand) 
       v.setVisibility(View.GONE); 
     } 

     @Override 
     public boolean willChangeBounds() 
     { 
      return true; 
     } 
    }; 
    a.setDuration(duration); 
    v.startAnimation(a); 
    return a; 
}