Android Design Support Library (2)-Use of textinputlayout

Source: Internet
Author: User

Original articles, reproduced please specify http://blog.csdn.net/leejizhou/article/details/50494634

This article describes the use of Textinputlayout in the Android Design support library, assuming that you do not yet know how to use this Design library please refer to http://blog.csdn.net/ Leejizhou/article/details/50479934,textinputlayout makes your edittext feel more material design. The EditText can be conveniently moved to the top of the message and can be easily prompted by error messages.

Don't say much nonsense. See effect:)

Control definition <android. Support. Design. Widgets. TextinputlayoutAndroid:id="@+id/tl_password"Android:layout_width="Match_parent"android:layout_height="Wrap_content"android:layout_margin="8DP"> <edittext android:id="@+id/password"Android:layout_width="Match_parent"android:layout_height="Wrap_content"Android:inputtype="Textemailaddress"Android:hint="Password"></EditText> </android. Support. Design. Widgets. Textinputlayout>

Textinputlayout is a parent container control. Wrapped EditText, there is no special attribute, use very easy, remember it must be used with the edittext.

Regular usage of textinputlayout

tl_password.setHint("Username"//EditText获得焦点后在上面显示的文字tl_password.setErrorEnabled(true//开启错误提醒tl_password.setError("密码不能为空!"//错误提醒的文字tl_password.setErrorEnabled(false//关闭错误提醒

Specific source code for the demo effect gif

Layout

<?xml version="1.0" encoding="utf-8"?

><linearlayout xmlns:android="Http://schemas.android.com/apk/res/android" Xmlns:tools="Http://schemas.android.com/tools"android:layout_width="Match_parent" android:layout_height="Match_parent"android:orientation="Vertical" Tools:context="com.leejz.textinputlayout.MainActivity"> <android.support.design.widget.textinputlayout android:id = "@+id/tl_username" android:layout_width =" match_parent " android:layout_height =" wrap_content " android:layout_margin =" 8DP "; <EditText android:id= "@+id/username"android:layout_width="Match_ Parent "android:layout_height="wrap_content "android:hint=" Username " > </EditText> </android.support.design.widget.TextInputLayout> <android.support.design.widget.textinputlayout android:id = "@+id/tl_password" android:layout_width =" match_parent " android:layout_height =" wrap_content " android:layout_margin =" 8DP "; <EditText android:id= "@+id/password"android:layout_width="Match_ Parent "android:layout_height="wrap_content "android:hint=" Password " > </EditText> </android.support.design.widget.TextInputLayout> <button android:layout_ Width = "match_parent" android:layout_height< /span>= "wrap_content" android:layout_margin = "18DP" android:text = "click" android:id = "@+id/button" /> <TextViewandroid:layout_width="Wrap_content"android:layout_height= "Wrap_content" android:layout_gravity="center"android:text="blog:http://blog.csdn.net/ Leejizhou " /> </linearlayout>

Activity

ImportAndroid.support.design.widget.TextInputLayout;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.text.TextUtils;ImportAndroid.view.View;ImportAndroid.widget.EditText; Public  class mainactivity extends appcompatactivity {    PrivateEditText username;PrivateEditText password;    Textinputlayout Tl_username; Textinputlayout Tl_password;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main);        Tl_username= (textinputlayout) Findviewbyid (r.id.tl_username); Tl_username.sethint ("Username");        Tl_password= (textinputlayout) Findviewbyid (R.id.tl_password); Tl_password.sethint ("Password");//Two ways to get EditText objects       //Username= (EditText) Findviewbyid (r.id.username);       //password= (EditText) Findviewbyid (R.id.password);Username=tl_username.getedittext (); Password=tl_password.getedittext ();//button ClickFindviewbyid (R.id.button). Setonclicklistener (NewView.onclicklistener () {@Override             Public void OnClick(View v) {if(Textutils.isempty (Password.gettext (). toString ())) {tl_password.seterrorenabled (true); Tl_password.seterror ("The password cannot be empty. "); }Else{tl_password.seterrorenabled (false);    }            }        }); }}

Ok, there are questions about the place can be in the comments below, thank you.

Android Design Support Library (2)-Use of textinputlayout

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.