1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
ImportNet. learningandroid. apitest. R;ImportAndroid. App. activity;ImportAndroid. OS. Bundle;ImportAndroid. View. gravity;ImportAndroid. View. layoutinflater;ImportAndroid. View. view;ImportAndroid. View. viewgroup;ImportAndroid. widget. textview;ImportAndroid. widget. Toast;/*** Toast usage overview. * @ author Bing **/public classToastsampleExtendsActivityImplementsView. onclicklistener {@ overrideProtected voidOncreate (bundle savedinstancestate ){Super. Oncreate (savedinstancestate); setcontentview (R. layout. toast_sample); findviewbyid (R. Id. btntoastnormal). setonclicklistener (This); Findviewbyid (R. Id. btntoastposition). setonclicklistener (This); Findviewbyid (R. Id. btntoastmargin). setonclicklistener (This); Findviewbyid (R. Id. btntoastcustomview). setonclicklistener (This) ;}@ OverridePublic voidOnclick (view v ){Switch(V. GETID ()){CaseR. Id. btntoastnormal:// The most common call method. The display duration is short. The value varies depending on the system. You can also specify the number of milliseconds. // The value of the 1st parameter is context, generally, you can specify the current activity instance directly. // 2nd parameters are the text to be displayed. Here, string is used directly, we recommend that you use the pre-defined string // 3rd parameters in XML to display the length, in milliseconds. the predefined toast is used here. length_short, // and toast. length_long can be used. These two values are slightly different depending on the system. // do not forget the last one. show ()Toast. maketext (This, "Toast text, normal", Toast. length_short). Show ();Break;CaseR. Id. btntoastposition:// The default gravity is gravity. center_horizontal | gravity. Bottom // adjust only the yoffset here, so that the text display position is closer to the nextToast t2 = toast. maketext (This, "Toast text with specific position", Toast. length_long); t2.setgravity (gravity. center_horizontal | gravity. Bottom, 0, 10); t2.show ();Break;CaseR. Id. btntoastmargin:// If You Want To adjust the display position significantly, we recommend that you use the setmargin method. // The parameters accepted by setmargin are the percentage of the horizontal and vertical values, respectively, in this way, the adaptability is better under different resolutions. // This is changed to display above the portrait center of the screenToast T3 = toast. maketext (This, "Toast text with specific margin and position", toast. length_short); t3.setgravity (gravity. center_horizontal | gravity. bottom, 0, 0); t3.setmargin (0f, 0.5f); t3.show ();Break;CaseR. Id. btntoastcustomview:// Use a custom view to display toast. You must first compile a layout definition file // In fact, the toast. maketext method is also called in this way.Layoutinflater Inflater = getlayoutinflater (); view layout = Inflater. Inflate (R. layout. toast_view_sample, (viewgroup) findviewbyid (R. Id. toastsamplelayout ),False); Textview text = (textview) layout. findviewbyid (R. id. toast_text); text. settext ("toast with custom view, it's a long text: "+" Manuka honey interferes with bacteria infecting a wound "+" by keeping the microbes from attaching to tissue "+" and even by making antibiotics more effective. "+" Cynthia Graber reports. "); toast t4 =NewToast (This); T4.setgravity (gravity. center_horizontal | gravity. Bottom, 0, 50); t4.setduration (toast. length_long); t4.setview (layout); t4.show ();Break;Default:Break;}}} |