Textinputlayout is a layout control in the new Android 5.0 feature--material design, which is primarily used to nest EditText for some of the effects of data entry, such as:
- When the input box gets the focus, the input prompt moves the animation to the top of the input box;
- When the input box loses focus, if there is no text in the input box, the prompt animation moves back into the input box;
- When the input is not the norm, the error message is displayed below the input box.
- When you enter a password, you can choose whether to display the "Show password" button and the button's pattern;
- You can display the length of the current text in the input box and the length of the request, and so on.
It is important to note that textinputlayout, as a layout control, cannot exist independently and must be nested within the edittext or textinputedittext outer layers.
As with other MD controls, you must declare dependencies in the Gradle file before using textinputlayout:
Compile ' com.android.support:design:25.0.0 '
1. Textinputlayout Properties:
app:passwordtoggleenabled: Whether to display the toggle button that can view the password app:passwordtoggledrawable: Picture of the toggle button to view the password Note: This icon is only displayed if the package's EditText or Textinputedittext inputtype is a password format app:counterenabled: Whether the text length counter app is displayed : Countermaxlength: Maximum length of text length counter Note: The text length counter is the word "x/Y" in the lower-right corner of the input box, x indicates the length of the text in the current input box, and Y indicates the specified input length If the user enters longer than Y, the text in the text counter changes color
2. Layout code example:
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:app= "Http://schemas.android.com/apk/res-auto"Android:id= "@+id/activity_main"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"> <!--wrapping EditText with Textinputlayout - <Android.support.design.widget.TextInputLayoutAndroid:id= "@+id/textinputlayout"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"app:counterenabled= "true"App:countermaxlength= "6"app:passwordtoggledrawable= "@mipmap/ic_launcher"app:passwordtoggleenabled= "true"> <!--the textinputedittext here can use EditText instead - <Android.support.design.widget.TextInputEditTextAndroid:id= "@+id/edittext"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:inputtype= "Textpassword" /> </Android.support.design.widget.TextInputLayout></LinearLayout>
Running results such as:
3. Error prompt:
Textinputlayout supports the error prompt, that is, when judged, the current input text does not meet the requirements, will be displayed below the input box a line of prompts, prompting for an error. This can be accomplished by invoking the seterrorenabled (), SetError () method of the Textinputlayout object. The specific code is as follows:
//Whether you can pop up the error messageTil.seterrorenabled (true); //get EditText of packages in TextinputlayoutEditText et =Til.getedittext (); //when the text in EditText is changed, the error message that handles Textinputlayout and its explicitEt.addtextchangedlistener (NewTextwatcher () {@Override Public voidBeforetextchanged (Charsequence S,intStartintCountintAfter ) {} @Override Public voidOnTextChanged (Charsequence S,intStartintBefore,intcount) { Try { if(". Equals (S +" ")) { //set the error prompt to NULL, that is, the error message is not displayedTil.seterror (NULL); } Else if(S.length () > 6) { //if the input length exceeds 6 bits, an error message is displayedTil.seterror ("Password length exceeds the upper limit! "); } Else{Integer.parseint (s+ ""); Til.seterror (NULL); } } Catch(Exception e) {//set the error prompt to specific textTil.seterror ("The input is not a number!") "); }} @Override Public voidaftertextchanged (Editable s) {}});
Demo Result:
The above is the basic usage of textinputlayout Introduction, the following sticker code in the cloud source code, for your reference.
Demo Address
Use of the Textinputlayout "ANDROID-MD"