2016-06-08 54 views
5

我使用TextInputLayout,并在其中设置drawableLeft,当我点击edittext时,标签已经浮起来了,但是我想用标签将左边的drawable浮起来。我如何实现,看到附加的图像...如何在TextInputLayout中使用(提示)标签浮动左边的drawable?

现在,当我点击“标题”只有leble浮起来,但我希望两者都应该浮动“左抽”和“标签”时的重点。之前点击它会看起来像“服务日期”的普通线。

enter image description here

我的代码如下..

<FrameLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/input_layout_cardnum" 
    > 
    <ImageView 
     android:id="@+id/imgLeft" 
     android:layout_width="30dp" 
     android:layout_height="30dp" 
     android:layout_gravity="bottom" 
     android:src="@mipmap/ic_launcher" 
     android:layout_marginBottom="10dp" 
     /> 
    <android.support.design.widget.TextInputLayout 
     android:id="@+id/input_layout_cardnums" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     > 
     <EditText 
      android:id="@+id/cardnumEditTexst" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:maxLength="255" 
      android:singleLine="true" 
      android:hint="hello" 
      android:drawableLeft="@null" 
      android:paddingLeft="35dp" 
      /> 
    </android.support.design.widget.TextInputLayout> 
</FrameLayout> 

而且在我的代码如此这般顶部标签

final Animation animation = AnimationUtils.loadAnimation(this,R.anim.anims); 
    final ImageView imgLeft = (ImageView) findViewById(R.id.imgLeft); 

    final EditText et = (EditText) findViewById(R.id.cardnumEditTexst); 
    et.setOnFocusChangeListener(new View.OnFocusChangeListener() 
    { 
     @Override 
     public void onFocusChange(View v, boolean hasFocus) 
     { 
      if (hasFocus) 
      { 
       if (et.getText().length() == 0) 
       { 
        imgLeft.startAnimation(animation); 
       } 
      } else 
      { 
       if (et.getText().length() == 0) 
        imgLeft.clearAnimation(); 
      } 
     } 
    }); 

而且我设置的聚焦状态的动画我动画会将图像视图置顶

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:fillAfter="true" 
    android:duration="200" 
    android:interpolator="@android:interpolator/linear" 
    > 
    <scale 
     android:fromXScale="0%" 
     android:fromYScale="0%" 
     android:toXScale="60%" 
     android:toYScale="60%" 
     android:pivotX="50%" 
     android:pivotY="50%" 
     /> 
    <translate 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:startOffset="100" 
     android:fromYDelta="0%" 
     android:toYDelta="-80%"/> 

</set> 

通过设置这个我得到了,现在漂浮是好的,但看到paddingLeft =“35dp”,我想从第0个位置开始看到第一个图像“标题”显示,我的意思是不应该是填充,但如果我会删除填充左边的浮动功能损失。

enter image description here

+1

你需要写你自己的实现。 TextInputLayout使用[CollapsingTextHelper](https://android.googlesource.com/platform/frameworks/support/+/6ba61c5/design/src/android/support/design/widget/CollapsingTextHelper.java)类将动画文本绘制到布局的帆布。由于这个课程是内部的,所以没有定制空间。 –

+0

@ S.D.我试图定制,但现在我只有一个小问题,我想要删除leftPadding,以便用户可以输入非常开始,请参阅图像 –

+0

只需在动画开始时删除/设置左侧绘制。 [TextInputLayout](https://android.googlesource.com/platform/frameworks/support/+/6ba61c5/design/src/android/support/design/widget/TextInputLayout.java)有一个TextListener监视其中的EditText,并且从这里开始动画。 –

回答

3

试试这个...

final Animation animation = AnimationUtils.loadAnimation(this, R.anim.anims); 
final ImageView imgLeft = (ImageView) findViewById(R.id.imgLeft); 

findViewById(R.id.cardnumEditTexst).setOnFocusChangeListener(new View.OnFocusChangeListener() { 
    @Override 
    public void onFocusChange(View v, boolean hasFocus) { 
     if (hasFocus) { 
      imgLeft.startAnimation(animation); 
     } else { 
      imgLeft.clearAnimation(); 
     } 
    } 
}); 

<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:duration="200" 
    android:fillAfter="true" 
    android:interpolator="@android:interpolator/linear"> 

    <scale 
     android:fromXScale="0%" 
     android:fromYScale="0%" 
     android:pivotX="50%" 
     android:pivotY="50%" 
     android:toXScale="60%" 
     android:toYScale="60%" /> 

    <translate xmlns:android="http://schemas.android.com/apk/res/android" 
     android:fromYDelta="0%" 
     android:startOffset="100" 
     android:toYDelta="-80%" /> 

</set> 

,并设置您的布局是这样的...

<FrameLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/input_layout_cardnum"> 

    <ImageView 
     android:id="@+id/imgLeft" 
     android:layout_width="30dp" 
     android:layout_height="30dp" 
     android:layout_gravity="bottom" 
     android:src="@mipmap/ic_launcher" 
     android:layout_marginBottom="10dp" /> 

    <android.support.design.widget.TextInputLayout 
     android:id="@+id/input_layout_cardnums" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <EditText 
      android:id="@+id/cardnumEditTexst" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:maxLength="255" 
      android:singleLine="true" 
      android:hint="hello" 
      android:drawableLeft="@null" 
      android:paddingLeft="35dp" /> 
    </android.support.design.widget.TextInputLayout> 
</FrameLayout> 
+0

请看我编辑的问题,我没有好转,看到图像。 –

相关问题