Android高级UI开发(十二)之 TextInputLayout用法

举报
yd_57386892 发表于 2020/12/29 00:58:04 2020/12/29
【摘要】 TextInputLayout见名知义与文本输入有关系,TextInputLayout控件通过内嵌EditText来实现输入文本时,根据预先设定的属性向用户展示相应的提醒文字并附有酷炫的动画效果。例如,当文本框里的字符长度大于10的时候自动给用户提示,无需编写额外的代码,同时文本框获取或失去焦点时,EditText的hint文本会动态显示和隐藏。下图为TextInputLay...

TextInputLayout见名知义与文本输入有关系,TextInputLayout控件通过内嵌EditText来实现输入文本时,根据预先设定的属性向用户展示相应的提醒文字并附有酷炫的动画效果。例如,当文本框里的字符长度大于10的时候自动给用户提示,无需编写额外的代码,同时文本框获取或失去焦点时,EditText的hint文本会动态显示和隐藏。下图为TextInputLayout结合EditText控件所实现的动态提醒效果:

1. 布局文件

     activity_main.xml


  
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. xmlns:app="http://schemas.android.com/apk/res-auto"
  6. android:orientation="vertical">
  7. <android.support.design.widget.TextInputLayout
  8. android:id="@+id/textInputLayout"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. app:hintAnimationEnabled="true"
  12. app:errorEnabled="true"
  13. >
  14. <EditText
  15. android:layout_width="fill_parent"
  16. android:layout_height="wrap_content"
  17. android:hint="请输入账号" />
  18. </android.support.design.widget.TextInputLayout>
  19. <EditText
  20. android:layout_width="fill_parent"
  21. android:layout_height="wrap_content"
  22. android:hint="请输入密码" />
  23. <Button
  24. android:layout_width="fill_parent"
  25. android:layout_height="wrap_content"
  26. android:hint="登录" />
  27. </LinearLayout>

布局分析:TextInputLayout内嵌一个EditText来实现具有动态效果的提醒给用户。

2. 代码

MainActivity.java


  
  1. package com.example.administrator.textinputlayout;
  2. import android.os.Bundle;
  3. import android.support.annotation.Nullable;
  4. import android.support.design.widget.TextInputLayout;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.text.Editable;
  7. import android.text.TextWatcher;
  8. public class Main2Activity extends AppCompatActivity {
  9. TextInputLayout textInputLayout;
  10. @Override
  11. protected void onCreate(@Nullable Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_main);
  14. initView();
  15. }
  16. public void initView()
  17. {
  18. textInputLayout = (TextInputLayout)findViewById(R.id.textInputLayout);
  19. //检测长度应该低于10位数
  20. textInputLayout.getEditText().addTextChangedListener(new MyTextWatcher(textInputLayout, "长度应低于10位数!"));
  21. //开启计数
  22. textInputLayout.setCounterEnabled(true);
  23. textInputLayout.setCounterMaxLength(10);//最大输入限制数
  24. return;
  25. }
  26. class MyTextWatcher implements TextWatcher {
  27. private String errorStr;
  28. private TextInputLayout textInputLayout;
  29. public MyTextWatcher(TextInputLayout textInputLayout, String errorStr) {
  30. // TODO Auto-generated constructor stub
  31. this.textInputLayout = textInputLayout;
  32. this.errorStr = errorStr;
  33. }
  34. @Override
  35. public void beforeTextChanged(CharSequence s, int start, int count,
  36. int after) {
  37. }
  38. @Override
  39. public void onTextChanged(CharSequence s, int start, int before,
  40. int count) {
  41. }
  42. @Override
  43. public void afterTextChanged(Editable s) {
  44. // 文字改变后回调
  45. if(s.toString().length()<=10){
  46. textInputLayout.setErrorEnabled(false);
  47. }else{
  48. textInputLayout.setErrorEnabled(true);
  49. textInputLayout.setError(errorStr);
  50. }
  51. }
  52. }
  53. }

代码分析:


  
  1. textInputLayout.setCounterEnabled(true);
  2. textInputLayout.setCounterMaxLength(10);//最大输入限制数

这两行代码开启了错误提醒,预设条件为输入字符数不能大于10,一旦textInputLayout控件内嵌的EditText超过10个字符,textInputLayout会自动提醒用户。

还有一种方法就是监听textInputLayout.getEditText()的输入字符的改变情况来判断字符数是否超过10个,核心代码如下:

textInputLayout.getEditText().addTextChangedListener(new MyTextWatcher(textInputLayout, "长度应低于10位数!"));
 

这里的代码比较简单,相信大家都可以看懂在此就不再赘述了。DEMO代码下载地址:

https://download.csdn.net/download/gaoxiaoweiandy/10704337

 

 

 

文章来源: blog.csdn.net,作者:冉航--小虾米,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/gaoxiaoweiandy/article/details/82957021

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。