The use of Android5.0 textinputlayout and Floatingactionbutton

Source: Internet
Author: User

Textinputlayout and Floatingactionbutton are MD-style controls that compare to normal edittext and button, ImageButton, Textinputlayout and Floatingactionbutton still have a lot of cool places, so let's take a look at it today.

1.TextInputLayout

Textinputlayout This control is mainly used in conjunction with EditText, previously in the edittext we often need to give edittext a hint property to tell the user what to enter here, but the user may lose to lose will forget what to enter here, It would be nice if there was a control that would solve the problem perfectly, so in Android M, Google launched the new textinputlayout, which perfectly solves the problem. OK, Next we'll see how this stuff is going to be used.

1. Add dependencies

Without a doubt, using textinputlayout we first have to add dependencies, adding the design package to our project, as follows:

[Java]View Plaincopy print?
    1. Compile ' com.android.support:design:23.1.1 '

2. Adding controls to the layout file

Next we need to add the relevant controls to the activity, we say that textinputlayout is used in conjunction with the EditText, the actual usage is to wrap the edittext with Textinputlayout, as follows:

[Java]View Plaincopy print?
  1. <android.support.design.widget.textinputlayout
  2. android:id="@+id/text_input_layout"
  3. Android:layout_width="Match_parent"
  4. android:layout_height="Wrap_content" >
  5. <edittext
  6. android:id="@+id/username"
  7. Android:layout_width="Match_parent"
  8. android:layout_height="48DP"/>
  9. </android.support.design.widget.TextInputLayout>


In fact, if there is no special need, you even EditText ID do not need to give, that some of the package may have doubts, do not give ID how do I get the content entered in EditText? Please look down.

Processing related logic in 3.Activity

Let's look at how to handle the logical operation of the input control in activity.

First find the control, and get to the edittext in the control, as follows:

[Java]View Plaincopy print?
    1. Textinputlayout = (textinputlayout) Findviewbyid (r.id.text_input_layout);
    2. EditText = Textinputlayout.getedittext ();


Set Hint:

[Java]View Plaincopy print?
    1. Textinputlayout.sethint ("Please enter your email address!")  ");


Please note that hint is for whom to set. Then I want users to enter an email address in edittext, but this email address I do not want to do too much verification, after all, I do not want to say that the regular, I just include an @ symbol is considered a legitimate mailbox, let's see how to achieve:

[Java]View Plaincopy print?
  1. Edittext.addtextchangedlistener (new Textwatcher () {
  2. @Override
  3. public void beforetextchanged (charsequence s, int. start, int count, int after) {
  4. }
  5. @Override
  6. public void ontextchanged (charsequence s, int start, int before, int count) { /c6>
  7. }
  8. @Override
  9. public void aftertextchanged (Editable s) {
  10. String S1 = s.tostring ();
  11. if (!s1.contains ("@")) {
  12. Textinputlayout.seterror ("illegal email address");
  13. Textinputlayout.seterrorenabled (true);
  14. }else{
  15. Textinputlayout.seterrorenabled (false);
  16. }
  17. }
  18. });


There is wood to feel very easy.

2.FloatingActionButton

Next we'll look at Floatingactionbutton.

1. Basic Properties

I talked about the use of the Coordinatorlayout Coordinator layout in my previous blog, and in the coordinator layout we could have a floatingactionbutton hanging on the AppBar, Then the Floatingactionbutton will slowly disappear with the folding of the head. Of course, the use of Floatingactionbutton is not confined here, we can also use the Floatingactionbutton in ordinary containers, as follows:

[Java]View Plaincopy print?
  1. <android.support.design.widget.floatingactionbutton
  2. Android:layout_width="Wrap_content"
  3. android:id="@+id/fab"
  4. android:onclick="Btnclick"
  5. app:backgroundtint="#ff0101"
  6. App:borderwidth="0DP"
  7. app:fabsize="Normal"
  8. app:elevation="10DP"
  9. android:src="@android:d rawable/ic_input_add"
  10. android:layout_height="Wrap_content"/>


Here are a few properties I need to introduce:

1.app:backgroundtint represents the background color of the fab (you can, of course, modify the fab background color by modifying the value of Coloraccent in Colors.xml)

2.app:borderwidth represents the ring width of the fab (after setting the width, when you press FAB, only the center part is pressed)

3.app:fabsize represents the size of the fab, with two values, normal and mini two

4.app:elevation indicates the z-axis height of the Fab

Display effect:


2. Modify the size

For the App:fabsize property described in 1 do not know cheese have no doubt, if you want to set the fab other size how to do it, let's take a look at the source code a little bit.

Open the source of fab, we found that Fab is inherited from ImageButton, the inside of the code is very understood, it has a method in the source code, as follows:

[Java]View Plaincopy print?
  1. Final int getsizedimension () {
  2. switch (msize) {
  3. Case Size_mini:
  4. return Getresources (). Getdimensionpixelsize (R.dimen.design_fab_size_mini);
  5. Case Size_normal:
  6. Default:
  7. return Getresources (). Getdimensionpixelsize (R.dimen.design_fab_size_normal);
  8. }
  9. }


It is obvious that the method is to get the size of the control, but we do not define these variables in the Dimen file in the Values folder in res, in fact the variables are in the design package we introduced, and the project view is adjusted to project, and we see the following directory:

The values file defines the two sizes of this control preset:

So if you want to redefine the Mini and normal values just need to overwrite these two values in your Dimens.xml file, as follows:

[Java]View Plaincopy print?
    1. <dimen name="Design_fab_size_mini" >80dp</dimen>
    2. <dimen name="Design_fab_size_normal" >112dp</dimen>


OK, it's easy.

Above.

The use of Android5.0 textinputlayout and Floatingactionbutton

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.