- 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