How to float left drawable with (hint) label in Te

2019-02-15 11:39发布

I am using TextInputLayout and in which i set a drawableLeft and when i click on edittext the label is already floating up but i want to float the left drawable too with the label. How can i achieve , see the attached image...

Right now when i click on "Title" only leble is floating up but i want both should be floating "leftdrawable" and "label" when focused. And before to click it will look a normal line like "Service Date".

enter image description here

My code is below..

<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>

And in my code i'm setting an animation on onFocus so it goes on top with label

  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();
            }
        }
    });

And my animation will throw the image view on top

<?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>

And by setting this i got, now floating is okay but see the paddingLeft="35dp", i want to start it from 0th position see in the first image where "Title" is showing, i mean there should not be padding but if i will remove the paddingLeft the floating functionality loss.

enter image description here

1条回答
小情绪 Triste *
2楼-- · 2019-02-15 12:29

try this...

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();
        }
    }
});

and

<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>

and set your layout like this...

<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>
查看更多
登录 后发表回答