Toast English name is toast, in Android This class is used to pop up the message, I think the SDK author thinks that the message is like a piece of toast. This theory does not have much to say, start our practice.
First step, design page
Main Page Toastview.xml
<?xml version= "1.0" encoding= "Utf-8"?>
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
android:orientation= "vertical" android:layout_width= "fill_parent"
android:layout_height= "Fill_parent" >
<button android:layout_height= "wrap_content" android:text= "Default" android:id= "@+id/btnsimpletoast" Android:layout_ Width= "Fill_parent" android:layout_alignparenttop= "true" android:layout_alignparentleft= "true" ></Button >
<button android:layout_height= "wrap_content" android:text= "Custom display location" Android:id= "@+id/ Btnsimpletoastwithcustomposition "android:layout_width=" fill_parent "android:layout_below=" @+id/btnSimpleToast " Android:layout_alignparentleft= "true" android:layout_margintop= "21DP" ></Button>
<button android:layout_height= "Wrap_content" android:text= "with Picture" android:id= "@+id/btnsimpletoastwithimage" Android:layout_width= "Fill_parent" android:layout_below= "@+id/btnsimpletoastwithcustomposition" Android:layout_ Alignparentleft= "true" android:layout_margintop= "20DP" ></Button>
<button android:layout_height= "Wrap_content" android:text= "Other Threads" android:id= "@+id/btnruntoastfromotherthread" Android:layout_width= "Fill_parent" android:layout_below= "@+id/btncustomtoast" android:layout_alignparentleft= " True "android:layout_margintop=" 26DP "></Button>
<button android:layout_height= "wrap_content" android:text= "fully customizable" android:id= "@+id/btncustomtoast" Android: Layout_width= "Fill_parent" android:layout_below= "@+id/btnsimpletoastwithimage" android:layout_alignparentleft= " True "android:layout_margintop=" 19DP "></Button>
</RelativeLayout>
customizing Toast pages Customtoast.xml
<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout
Xmlns:android= "Http://schemas.android.com/apk/res/android"
android:layout_height= "Wrap_content" android:layout_width= "Wrap_content"
Android:background= "#ffffffff" android:orientation= "vertical"
Android:id= "@+id/lltoast" >
<textview android:layout_height= "Wrap_content"
Android:layout_margin= "1dip" android:textcolor= "#ffffffff"
Android:layout_width= "fill_parent" android:gravity= "center"
Android:background= "#bb000000" android:id= "@+id/tvtitletoast"/>
<linearlayout android:layout_height= "Wrap_content"
android:orientation= "vertical" android:id= "@+id/lltoastcontent"
android:layout_marginleft= "1dip" android:layout_marginright= "1dip"
Android:layout_marginbottom= "1dip" android:layout_width= "Wrap_content"
android:padding= "15dip" android:background= "#44000000" >
<imageview android:layout_height= "Wrap_content"
android:layout_gravity= "center" android:layout_width= "Wrap_content"
Android:id= "@+id/imgtoast"/>
<textview android:layout_height= "Wrap_content"
android:paddingright= "10dip" android:paddingleft= "10dip"
Android:layout_width= "wrap_content" android:gravity= "center"
Android:textcolor= "#ff000000" android:id= "@+id/tvtexttoast"/>
</LinearLayout>
</LinearLayout>
The second step, design acttivity Toastactivity.java
/**
*
*/
Package Com.figo.helloworld;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.os.Handler;
Import android.view.Gravity;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.view.ViewGroup;
Import Android.widget.ImageView;
Import Android.widget.LinearLayout;
Import Android.widget.TextView;
Import Android.widget.Toast;
/**
* @author Administrator
*
*/
public class Toastactivity extends Activity implementsonclicklistener {
Handler Handler = new Handler ();//help the main thread and child threads communicate
@Override
public void OnCreate (bundlesavedinstancestate) {
Super.oncreate (savedinstancestate);
Find page
Setcontentview (R.layout.toastview);
Add a button Event
Findviewbyid (R.id.btnsimpletoast). Setonclicklistener (this);
Findviewbyid (r.id.btnsimpletoastwithcustomposition). Setonclicklistener (
this);
Findviewbyid (r.id.btnsimpletoastwithimage). Setonclicklistener (this);
Findviewbyid (R.id.btncustomtoast). Setonclicklistener (this);
Findviewbyid (R.id.btnruntoastfromotherthread). Setonclicklistener (this);
}
public void Showtoast () {
Handler.post (New Runnable () {
@Override
public void Run () {
Toast.maketext (Getapplicationcontext (), "I'm from another thread!" ",
Toast.length_short). Show ();
}
});
}
@Override
public void OnClick (View v) {
Toast toast = null;
Switch (V.getid ()) {
Case r.id.btnsimpletoast://Simple Toast
Toast.maketext (Getapplicationcontext (), default toast style,
Toast.length_short). Show ();
Break
caser.id.btnsimpletoastwithcustomposition://A toast of a custom location
Toast =toast.maketext (Getapplicationcontext (), "Custom Location Toast",
Toast.length_long);
Toast.setgravity (gravity.center,0, 0);
Toast.show ();
Break
caser.id.btnsimpletoastwithimage://a toast with pictures
Toast =toast.maketext (Getapplicationcontext (), "toast with pictures",
Toast.length_long);
Toast.setgravity (gravity.center,0, 0);
LinearLayout Toastview = (linearlayout) Toast.getview ();
ImageView imagecodeproject= New ImageView (Getapplicationcontext ());
Imagecodeproject.setimageresource (R.drawable.icon);
Toastview.addview (imagecodeproject,0);
Toast.show ();
Break
Case r.id.btncustomtoast://Custom Toast
Layoutinflaterinflater = Getlayoutinflater ();
View layout =inflater.inflate (R.layout.customtoast,
(ViewGroup) Findviewbyid (r.id.lltoast));
ImageView image = (ImageView) layout
. Findviewbyid (R.id.imgtoast);
Image.setimageresource (R.drawable.icon);
TextView title = (TextView) Layout.findviewbyid (r.id.tvtitletoast);
Title.settext ("Attention");
TextView Text = (TextView) Layout.findviewbyid (r.id.tvtexttoast);
Text.settext ("Fully customizable toast");
Toast = Newtoast (Getapplicationcontext ());
Toast.setgravity (gravity.right| Gravity.top, 100, 60);
Toast.setduration (Toast.length_long);
Toast.setview (layout);
Toast.show ();
Break
caser.id.btnruntoastfromotherthread://show toast on the main thread after other threads have finished executing
New Thread (New Runnable () {
public void Run () {
Showtoast ();
}
}). Start ();
Break
}
}
}
The third step, Androidmanifest.xml registered activity
<activity android:name= ". Toastactivity "android:label=" @string/app_name ">
<intent-filter>
<action android:name= "Android.intent.action.MAIN"/>
<category android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
Fourth step, Operation effect
Android Development Step by step 32: Play a Toast