1: Select a control to trigger
2: Trigger event to listen, then bind Toast object for message prompt
1, when creating an Android project, bring your own activity, and we look at the code
1 PackageCom.example.androidday2testbyteacherbydfs;2 3 Importandroid.app.Activity;4 Importandroid.content.Intent;5 ImportAndroid.os.Bundle;6 ImportAndroid.view.View;7 ImportAndroid.view.View.OnClickListener;8 ImportAndroid.widget.Button;9 Ten One Public classMainactivityextendsActivity { A - PrivateButton butn; - the @Override - protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); - //to set the layout + Setcontentview (r.layout.mytest); - //returns a Button control object. +BUTN =(Button) Findviewbyid (r.id.mybuttonone); AButn.settext ("You, point me to see ..."); at - - //This is an anonymous inner class that implements a click event to prompt for information. - /* - Butn.setonclicklistener (New Onclicklistener () { - //Create a new anonymous inner class in @Override - Public void OnClick (View arg0) { to Toast ss = Toast.maketext (Mainactivity.this, "welcome you to learn Android", Toast.length_long); + ss.show (); - } the });*/ * $ //the external class is now referenced to implement the click Information (for easy maintenance purposes)Panax Notoginseng -Butn.setonclicklistener (NewMytestonclickone ( This)); the + A } the}
It already has an implementation,-------anonymous inner class,
Look at the structure:
Now I second is the realization of the way, the external class is convenient maintenance
Create a Mytestonclickone This class implements the interface of the Click event as follows:
1 PackageCom.example.androidday2testbyteacherbydfs;2 3 4 5 Importandroid.app.Activity;6 ImportAndroid.content.Context;7 Importandroid.content.Intent;8 ImportAndroid.view.View;9 ImportAndroid.view.View.OnClickListener;Ten ImportAndroid.widget.EditText; One ImportAndroid.widget.Toast; A /** - * Build a Listener class for button click events - * @authorAdministrator the * - */ - Public classMytestonclickoneextendsActivityImplementsOnclicklistener { - + PrivateContext con; - Publicmytestonclickone (Context con) { + This. con=con; A at } - - @Override - Public voidOnClick (View arg0) { - //build a toast that corresponds to the alert of the webpage. Maketext (Contextual context object, string string, showing the time of the delay); -Toast ts = Toast.maketext (Con, "Welcome to your presence!"), Toast.length_long); in ts.show ();//This is open meaning, that is, the meaning of the call. - toIntent Intent =NewIntent (con,mytestredirectpage.class); + - con.startactivity (intent); the * } ; $ Panax Notoginseng}
When a run-time effect
----------------------------------------------------------------------------------------------
The above is the full content of the toast message prompt.
Tips for Android messages, toast Toast Way