Android Material Design -TextInputLayout
来源:互联网 发布:淘宝达人账号如何注销 编辑:程序博客网 时间:2024/06/07 01:47
Android Material Design 有一个重要的控件 —-TextInputLayout
我先上一下TextInputLayout的布局东西
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/activity_main" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.king_mi.lottery.MainActivity"> <TextView android:id="@+id/tv_result" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="还未开奖" android:textSize="30sp" android:layout_margin="20dp" android:textColor="@color/colorAccent" /> <android.support.design.widget.TextInputLayout android:id="@+id/til_value_a" android:layout_width="match_parent" android:layout_height="wrap_content" app:counterEnabled="true" app:counterMaxLength="15" android:hint="请输入数值A" > <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="number"/> </android.support.design.widget.TextInputLayout> <android.support.design.widget.TextInputLayout android:id="@+id/til_value_b" android:layout_width="match_parent" android:layout_height="wrap_content" app:counterEnabled="true" app:counterMaxLength="15" android:hint="请输入数值B" > <android.support.design.widget.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="number" /> </android.support.design.widget.TextInputLayout> <android.support.design.widget.TextInputLayout android:id="@+id/til_value_times" android:layout_width="match_parent" android:layout_height="wrap_content" app:counterEnabled="true" app:counterMaxLength="15" android:hint="请输入参数人次" > <android.support.design.widget.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="number" /> </android.support.design.widget.TextInputLayout> <Button android:id="@+id/btn_ok" android:layout_marginTop="30dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="获取开奖结果" /></LinearLayout>
上面可以看到我用了TextInputLayout的东西
1. TextInputLayout 和Scrollview一样,只能包含一个子控件
2. TextInputLayout 中只能包含TextInputEditText或EditText,他们的效果是一样的
3. TextInputLayout 中cunterMaxLength 是提示可以输入最大的次数,仅提示作用.
4. TextInputLayout 中hint 可以在布局文件与代码中设置,但不要同时设置,以防重叠.建议直接在布局文件里面设置好.
@OnClick(R.id.btn_ok) public void onClick() { String valueA = tilValueA.getEditText().getText().toString().trim(); String valueB = tilValueB.getEditText().getText().toString().trim(); String valueTimes = tilValueTimes.getEditText().getText().toString().trim(); if (doCheck(valueA, valueB, valueTimes)) { getResult(valueA, valueB, valueTimes); } }
- 这里注意获取Edittext中的值,我们不需要去声明EditText控件.
重点来了
private boolean doCheck(String valueA, String valueB, String valueTimes) { boolean check = true; // if (TextUtils.isEmpty(valueA)) { tilValueA.setError("请输入数值A"); tilValueA.setErrorEnabled(true); check = false; } else { tilValueA.setErrorEnabled(false); } if (TextUtils.isEmpty(valueB)) { tilValueB.setError("请输入数值B"); check = false; } else { tilValueB.setError(null); } if (TextUtils.isEmpty(valueTimes)) { tilValueTimes.setError("请输入参与人次!"); check = false; } else { tilValueTimes.setError(null); } return check; }
- setError(“XXX”) 可以在输入控件的下方做一个错误的提示,用setError(null) 来清除这个提示.
- setErrorEnable(true)与setErrorEnable(false)试了多次没有发现有啥作用,如果说有作用,就是setErrorEnable(false)和setError(null)一样可以清除错误标记
- 个人建议只需要使用setError就可以了,setErrorEnable没有啥作用.
0 0
- Android Material Design -TextInputLayout
- Android Material Design之TextInputLayout
- Android Material Design之TextInputLayout
- Android Material Design 之 TextInputLayout
- Android Material Design系列之TextInputLayout
- Android Material Design系列之TextInputLayout
- Android Material Design之TextInputLayout、TextInputEditText解析
- Android:Material Design之TextInputLayout使用
- 浅析Android Material Design之TextInputLayout
- Material Design之TextInputLayout
- Material Design -- TextInputLayout
- Material Design之TextInputLayout
- Material Design之TextInputLayout
- Material Design之TextInputLayout
- Android进阶——Material Design新控件之TextInputLayout
- Android TextInputLayout,打造 Material Design 风格的文本输入框
- Android开发——Material Design系列之TextInputLayout
- Material Design系列之TextInputLayout
- HDU 1078 FatMouse and Cheese
- #!/usr/bin/env python的作用
- Spring源码学习之AOP
- 运维笔记31 (pacemaker高可用集群搭建的总结)
- LeetCode-496. Next Greater Element I
- Android Material Design -TextInputLayout
- 公众号第三方平台和微信公众号平台的区别与开发步骤
- 程序中乱码解决方案
- 集合各实现类的底层实现原理
- 设置mysql远程链接
- Redis设计思路学习与总结
- Vue2+VueRouter2+webpack 构建项目实战(二)目录以及文件结构
- Win10获取VSS、CVS服务器的代码很慢
- nginx问题定位之监控进程异常退出