zoukankan      html  css  js  c++  java
  • Android学习之Design Support Library中TextInputLayout的使用

    今天学习了一个Android Design Support Library 中的TextInputLayout控件,感觉还不错,较之以往的Editetxt,多了几分灵活性,使用也非常easy,故此给大家推荐一下。并记录用法。

    首先上图来介绍一下它跟我们以往使用的有什么改变。

    这里写图片描写叙述

    这里简单看一下,它多了一个提示功能。一般当EditText输入内容后,hint会消失。

    TextInputLayout会在输入内容后。提示就会浮动在EditText上方。

    当然也支持错误提示。

    以下来看实现方式

    加入依赖

        //noinspection GradleCompatible
        compile 'com.android.support:design:24.0.0-alpha1'

    该怎样实现呢?TextInputLayout作为父布局包括EditText即可了

     <android.support.design.widget.TextInputLayout
                    android:id="@+id/textInputLayouts"
                    android:layout_width="match_parent"
                    android:layout_margin="8dp"
                    android:layout_height="wrap_content">
    
                    <EditText
                        android:id="@+id/etPassword"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="@dimen/space_16"
                        android:hint="请输入password"
                        android:inputType="textPassword"
                        android:padding="@dimen/space_16"
                        android:textColor="@color/grey_800"
                        android:textSize="@dimen/text_size_14" />
                </android.support.design.widget.TextInputLayout>

    实现方式非常easy哈。然后该怎样在代码里设置呢?

    TextInputLayout主要包括以下几个方法:


    1.setHint()。设置提示信息

    2.setError(),设置错误提示信息

    3.setErrorEnabled(boolean enabled),设置error是否显示

    4.getEditText(),获取自布局EditText

    5.setCounterMaxLength(int) 设置可输入最大字符数。并在以下进行统计

    6.setCounterEnabled(boolean ); 设置是否统计输入字符数

    7.setHintAnimationEnabled(boolean) 设置hint动画是否开启。


    这里写图片描写叙述

    效果如上。

     tlName = (TextInputLayout) findViewById(R.id.textInputLayout);
     tlPwd = (TextInputLayout) findViewById(R.id.textInputLayouts);
     tlName.setHint("请输入username");
     tlPwd.setHint("请输入password");
    
                    String userName = etUserName.getText().toString().trim();
                    String password = etPassword.getText().toString();
                    if (TextUtils.isEmpty(userName)) {
                        tlName.setError("username不能为空");
                    } else {
                        tlName.setErrorEnabled(false);
                    }
                    if (TextUtils.isEmpty(password)) {
                        tlPwd.setError("password不能为空");
                    } else {
                        tlPwd.setErrorEnabled(false);
                    }

    逻辑嘛。也就是我们寻常写的登陆注冊逻辑。关键点在于加入了setError()。setErrorEnabled()方法。

    好啦,就这么多。有个问题在设置setCounterMaxLengt()后。输入超过设置字符数后,会数组越界,不知道哪位大兄弟能够教我,谢谢!

    代码下载

    http://pan.baidu.com/s/1pKDg2a3

  • 相关阅读:
    C#设计模式——抽象工厂模式(原文转自:http://blog.jobbole.com/78059/)
    WebConfig配置文件详解(转载自逆心的博客)
    ASP.NET C# 日期 时间 年 月 日 时 分 秒 格式及转换(转自happymagic的专栏)
    ASP.NET RepeatLayout 属性
    牛顿迭代法
    汉诺塔(整理)
    游戏引擎---好牛(转)
    字符串相关面试题(整理)
    有关java调用批处理文件
    有关java 8
  • 原文地址:https://www.cnblogs.com/yangykaifa/p/7356845.html
Copyright © 2011-2022 走看看