InProgramThe steps for creating a toast are as follows:
1. Call the static method maketext () of toast to add real text and time length.
2. Call toast's show () display.
Example:
/Chapter04_ui_toast/src/COM/Amaker/test/mainactivity. Java
Code
Package Com. Amaker. test;
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. widget. Button;
Import Android. widget. Toast;
Public Class Mainactivity Extends Activity {
/** Called when the activity is first created. */
Private Button B1, B2;
@ Override
Public Void Oncreate (bundle savedinstancestate ){
Super . Oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
B1 = (Button) findviewbyid (R. Id. button01 );
B2 = (Button) findviewbyid (R. Id. button02 );
Final Int L = Toast. length_long;
Final Int S = Toast. length_short;
Final String S1 = " I will show more for a while! " ;
Final String S2 = " I will show it for a while! " ;
B1.setonclicklistener ( New Onclicklistener (){
Public Void Onclick (view v ){
Toast T1 = Toast. maketext (getapplicationcontext (), S1, L );
T1.show ();
}
});
B2.setonclicklistener ( New Onclicklistener (){
Public Void Onclick (view v ){
Toast T2 = Toast. maketext (getapplicationcontext (), S2, S );
T2.show ();
}
});
}
}