Optimization of a prompt box custom toast Component (ii) Business logic implementation of toast component

Source: Internet
Author: User

    • Under Java org.socrates.mydiary.activity under Loginactivity, customize a Method Showcustomertoast ()?
1  Public classLoginactivityextendsappcompatactivity {2      Private voidShowcustomertoast (Final intIconFinalString message) {3Layoutinflater Inflater=getlayoutinflater ();//Create an Layoutinflater interface object by getting a Layoutinflater object4View layout=inflater.inflate (R.layout.toast_customer, (ViewGroup) Findviewbyid (r.id.toast_layout_root));//Use the Inflater method in the Inflater object to bind the layout file of the custom toast, pointing to the layout file with the tag node5  6ImageView toasticon=(ImageView) Layout.findviewbyid (R.id.toasticon);7 toasticon.setbackgroundresource (icon);8  9TextView toastmessage = (TextView) Layout.findviewbyid (r.id.toastmessage);//gets the TextView component in the layout file and assigns it a value dynamicallyTen toastmessage.settext (message); One   AToast toast=NewToast (Getapplicationcontext ());//instantiating a Toast Component Object - toast.setduration (Toast.length_long);  -Toast.setview (layout);////bind a set custom layout to the current Toast object theToast.show ();//Show Toast Components -      } - } -  

Business logic Flow:

(1) Create a Layoutinflater interface object by getting the Layoutinflater object

(2) Use the Inflater method in the Inflater object to bind the layout file of the custom toast, pointing to the layout file with the tag node

(3) Get the TextView component in the layout file and assign it a value dynamically

(4) Instantiating a Toast Component object

(5) Bind a set custom layout to the current Toast object

(6) Show Toast component

    • Call this method at the specified location
1 Private  classViewoclImplementsview.onclicklistener{2 @Override3          Public voidOnClick (View v) {4             Switch(V.getid ()) {5                  CaseR.id.btnlogin:6String account=Txtaccount.gettext (). toString (). Trim ();7String password=Txtpassword.gettext (). toString (). Trim ();8                     BooleanLogin_flag =false;9                             Ten                     if(login_flag) { OneShowcustomertoast (Android. R.drawable.ic_menu_call, "Welcome login," + account);//call this method at the specified location A                 -                          Break; -                the                     } -                     Else { -Showcustomertoast (Android. R.drawable.ic_delete, "Incorrect account or password");//call this method at the specified location -                     } +                      Break; -              } +         } A}

Run:

Optimization of a prompt box custom toast Component (ii) Business logic implementation of toast component

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.