前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android - 自定义View,实现不一样的输入框

Android - 自定义View,实现不一样的输入框

作者头像
android_薛之涛
发布2018-09-12 10:52:05
1.4K0
发布2018-09-12 10:52:05
举报
文章被收录于专栏:Android-薛之涛

最近看到一个效果,就是在登陆界面输入账号密码的时候,会有一个动画效果,感觉不错,找了一些资料,学习了一下。已经实现效果,效果如下:

undefined_腾讯视频

ok,首先先分析一下这个输入框(账号和密码是一样的),上代码:

<RelativeLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="vertical"

>

<!--  顶部文本控件 -->

<TextView

android:id="@+id/tv_topmessage"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

    android:textSize="14sp"/>

<Relativelayout

android:layout_width="match_parent"

android:layout_height="50dp"

android:layout_below="@+id/tv_topmessage"

>

<!--底部文本控件-->

<TextView

    android:id="@+id/tv_bottomMessage"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:clickable="false"

android:visibility="invisible"

android:gravity="center_vertical"

android:layout_centerVertical="true"

android:textSize="16sp"

    />

<!-- 输入框-->

<EditText

android:id="@+id/edt_content"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_alignParentStart="true"

android:layout_alignParentTop="true"

android:background="@null"

android:textColor="#2A2A2A"

android:textColorHint="#999999"

android:textSize="16sp"/>

<!-- 输入框底部view--->

 <View  android:id="@+id/lineview"

android:layout_width="match_parent"

android:layout_height="1dp"

android:layout_alignParentBottom="true"

    android:background="#EBEBEB" />

</RelativeLayout>

</RelativeLayout>

布局的效果是这样的,实际情况是我选择的是wrap-content,这样是为了便于大家理解:

先说一下思路:

首先要自定义属性:

   注释很明确,不解释。。。。

提示如何根据自定义属性获取属性值:TypedArray.。

情况一:

在刚进入界面的时候,EditText没有内容,让TextViewA,移动到TextViewB的位置,TextViewA显示内容,比如:请输入密码(这里用到了自定义的属性值bottomMessage)。

情况二:

当EditText输入内容的时候,TextViewA动画回退到之前的位置,重新设置内容,比如:密码(这里用到了自定义的属性值topMessage)。

重点:如何计算顶部TextViewA移动的到TextViewB的位置或者回退到顶部的移动距离呢?

就是重写onWindowFocusChanged()方法,自定义控件初始化完毕,可通过getLocationInWindow(),获取各控件的位置信息,也就是TextView的x值y值,我这里用了两个数组分别记录A,B。,既然获取了坐标,那A移动的距离就是B的Y坐标减A的Y坐标,X坐标嘛,都一样。

最后一步动画:传入相关参数即可。

已上传github: https://github.com/searchdingding/CustomViewDemo

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018.04.08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 布局的效果是这样的,实际情况是我选择的是wrap-content,这样是为了便于大家理解:
  • 重点:如何计算顶部TextViewA移动的到TextViewB的位置或者回退到顶部的移动距离呢?
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档