Use of Android------toast

Source: Internet
Author: User
<span id="Label3"></p><p><p>Originally thought that Toast has only then one simple function, is Toast.maketext (context, text, duration). show (); that's it.</p></p><p><p>But a few days ago, the problem was that it could not be used in a sub-thread, so it looked at the use of the Toast. Discover toasts can also be customized for location display, with picture display, and, of course, in child threads.</p></p><p><p></p></p><p><p></p></p><p><p>An example Illustrates:</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>Layout file:</p></p><p><p></p></p><pre class="html" name="code"><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" fill_parent "android:layout_height=" fill_parent "android:orienta tion= "vertical" android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_ Horizontal_margin "android:paddingright=" @dimen/activity_horizontal_margin "android:paddingtop=" @dimen/activity_ Vertical_margin "tools:context=" com.example.toasttest.MainActivity "> <button android:id=" @+id/bt1 " Android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:text= "default"/> <B Utton android:id= "@+id/bt2" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:text= "custom display location"/> <button android:id= "@+id/bt3" android:layout_width= "fill_parent" android:layout_height= "wrap_content" Android:text= "with picture"/> <button android:id= "@+id/bt4" android:layout_width= "fill_parent" android:layout _height= "wrap_content" android:text= "in other threads"/></linearlayout></pre><p><p><br>Activity:</p></p><p><p></p></p><pre class="html" name="code">public class Mainactivity extends Activity implements Onclicklistener {Button bt1, bt2, bt3, bt4, bt5; @Overrideprotected V OID onCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); setcontentview (r.layout.activity_main ); bt1 = (button) Findviewbyid (r.id.bt1); bt2 = (button) Findviewbyid (r.id.bt2); bt3 = (button) Findviewbyid (r.id.bt3); bt4 = (Button) Findviewbyid (r.id.bt4), bt1.setonclicklistener (this), bt2.setonclicklistener (this); Bt3.setonclicklistener (this); Bt4.setonclicklistener (this);} @Overridepublic void OnClick (View v) {Toast toast;switch (v.getid ()) {case r.id.bt1://is displayed by default Toast.maketext ( mainactivity.this, "default display", 1). show (); break;case r.id.bt2://custom display location Toast = toast.maketext (mainactivity.this, "custom display location ", 1); toast.setgravity (gravity.top, 0, 0); toast.show (); break;case r.id.bt3://with Picture Show toast = Toast.maketext ( mainactivity.this, "with picture display", 1); toast.setgravity (gravity.center, 0, 0); LinearLayout Toastview = (linearlayout) Toast.getview (); ImageView ImageView = new ImageView (getApplicationContext ()); imageview.setimageresource (r.drawable.pig); toastview.addview (imageView); toast.show (); Break;case r.id.bt4://displays The new thread (new Runnable () {@Overridepublic void run () {looper.prepare () in other threads; Toast.maketext (mainactivity.this, "using toast in thread", 1). show (); Looper.loop ();}}). Start (); break;default:break;}}}</pre><p><p><br></p></p><p><p></p></p><p><p></p></p><p><p>Use of Android------toast</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.