您的位置:首页 > 移动开发 > Android开发

利用TextInputLayout实现hint浮动显示(一)

2016-12-17 17:40 429 查看
学习平台:Android Studio2.2.3(基于win8.1 Pro)

ADT:Nexus5

API:24

参考书籍:《第一行代码》2th edition

有关章节:3.2 常用控件的使用方法。

关于hint提示,郭老师的第一版书使用的是EditText的hint,默认有浅色的文字提示,确实比没有任何提示好。但是我也见过另一种hint,用户输入时会自动到输入框左上角,所以看到3.2节时,我希望有我想要的改变,结果,没有变化。

我以为新型的hint仍然是EditText的属性,所以查看官方文档

浏览一遍,没有找到我需要的内容,所以直接Google:hint Android,知道了这种hint叫floating hint,但是搜索到的是一个视频教程,所以Google 关键词floating hint。找到有关教程Android Material Design Floating Labels for EditText

原来是需要TextInputLayout

下面是我使用TextInputLayout的步骤:

第一步:添加Support Library

1. 在Android Studio中,File–>Project Structure–>app–>Dependencies–添加Library dependency



2. 搜索design,找到com.android.support:design:25.0.1。

第二步:修改布局文件

将EditText包含于TextInputLayout内层。

如下:

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

<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Type something here" />
</android.support.design.widget.TextInputLayout>


确实,hint到了左上角



但是浮动太快,来不及看,以为运行时,hint就直接出现于输入框左上角了呢!

不过没有关系,我又添加了一个TextInputLayout。

以下是效果图



以上就是我对EditText的进一步学习。

另外,一点说明,我开始写博客,并不是单纯地记录实现某些功能或完成某个项目的步骤,我更是为了积累自己的这个过程,所以相对有些啰嗦。

昨晚添加TextInputLayout时,一直没有出现我想要的浮动效果,所以就以为自己的代码出了问题,又继续去参考别人的文章,确实找到一篇,如果需要,请看下一篇–《利用TextInputLayout实现hint浮动显示(二)》。谢谢!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息