码迷,mamicode.com
首页 > 其他好文 > 详细

TextInputLayout输入框验证

时间:2018-02-16 20:26:11      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:item   prot   sign   log   技术   edittext   信息   文本   ddt   

 技术分享图片

 1 <!--
 2 通过修改<color name="colorAccent">#023cfa</color>可以修改正确提示文本的颜色
 3 添加<item name="android:textColorPrimary">@color/textColorPrimary</item>属性可以修改输入文本的颜色
 4 -->
 5 <android.support.design.widget.TextInputLayout
 6     android:id="@+id/text_input_layout"
 7     android:layout_width="match_parent"
 8     android:layout_height="wrap_content">
 9 
10     <EditText
11         android:layout_width="match_parent"
12         android:layout_height="48dp"/>
13 </android.support.design.widget.TextInputLayout>

 

 1 public class MainActivity extends AppCompatActivity {
 2 
 3     private TextInputLayout textInputLayout;
 4 
 5     @Override
 6     protected void onCreate(Bundle savedInstanceState) {
 7         super.onCreate(savedInstanceState);
 8         setContentView(R.layout.activity_main);
 9         textInputLayout = (TextInputLayout) findViewById(R.id.text_input_layout);
10         textInputLayout.setHint("请输入邮箱地址");
11         //获取到TextInputLayout包裹的EditText
12         EditText editText = textInputLayout.getEditText();
13         editText.addTextChangedListener(new TextWatcher() {
14             @Override
15             public void beforeTextChanged(CharSequence s, int start, int count, int after) {
16 
17             }
18 
19             @Override
20             public void onTextChanged(CharSequence s, int start, int before, int count) {
21 
22             }
23 
24             @Override
25             public void afterTextChanged(Editable s) {
26                 //如果不包含@认为是非法邮箱地址
27                 if (!s.toString().contains("@")) {
28                     //允许TextInputLayout显示错误信息
29                     textInputLayout.setErrorEnabled(true);
30                     //设置错误信息
31                     textInputLayout.setError("邮箱地址非法");
32                 } else {
33                     textInputLayout.setErrorEnabled(false);
34                 }
35             }
36         });
37     }
38 }

 

TextInputLayout输入框验证

标签:item   prot   sign   log   技术   edittext   信息   文本   ddt   

原文地址:https://www.cnblogs.com/ganchuanpu/p/8450415.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!