Toast notifications are the simplest message notifications in Android. Next show how to use the toast notification.
1. A new project, Toast.
2. Code in the Main.xml.
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"
xmlns:tools= "http:// Schemas.android.com/tools "
android:layout_width=" match_parent "
android:layout_height=" Match_parent " >
<button
android:id= "@+id/button"
android:layout_width= "wrap_content"
android:layout_ height= "Wrap_content"
android:layout_centerhorizontal= "true"
android:layout_centervertical= "true"
android:padding= "@dimen/padding_medium"
android:text= "@string/hello_world"/>
</ Relativelayout>
3. Code in the Mainactivity.java.
public class Mainactivity extends activity {
Private button button;
@Override public
void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.main);
Button = (button) Findviewbyid (R.id.button);
Button.setonclicklistener (New Onclicklistener () {
@Override public
void OnClick (View v) {
Toast.maketext (Getbasecontext (), "Hi, I am a Toast",
toast.length_short). Show ();}}
}
4. Press F11 to debug on the simulator. Click on the Center button, it will pop up the toast notice.
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/