Android Toast的用法总结(五种用法)
程序员文章站
2024-02-21 17:04:40
toast大家都很熟,不多说。直接上图上代码。
具体代码如下:
main.xml:
toast大家都很熟,不多说。直接上图上代码。
具体代码如下:
main.xml:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:orientation="vertical" android:padding="5dip" > <button android:id="@+id/btnsimpletoast" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="默认" > </button> <button android:id="@+id/btnsimpletoastwithcustomposition" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="自定义显示位置" > </button> <button android:id="@+id/btnsimpletoastwithimage" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="带图片" > </button> <button android:id="@+id/btncustomtoast" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="完全自定义" > </button> <button android:id="@+id/btnruntoastfromotherthread" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="其他线程" > </button> </linearlayout>
custom.xml:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/lltoast" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#ffffffff" android:orientation="vertical" > <textview android:id="@+id/tvtitletoast" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="1dip" android:background="#bb000000" android:gravity="center" android:textcolor="#ffffffff" /> <linearlayout android:id="@+id/lltoastcontent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginbottom="1dip" android:layout_marginleft="1dip" android:layout_marginright="1dip" android:background="#44000000" android:orientation="vertical" android:padding="15dip" > <imageview android:id="@+id/tvimagetoast" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> <textview android:id="@+id/tvtexttoast" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:paddingleft="10dip" android:paddingright="10dip" android:textcolor="#ff000000" /> </linearlayout> </linearlayout>
package com.example.test; import android.app.activity; import android.app.actionbar; import android.app.fragment; import android.content.intent; import android.os.bundle; import android.os.handler; import android.view.gravity; import android.view.layoutinflater; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.view.onclicklistener; import android.view.viewgroup; import android.widget.button; import android.widget.edittext; import android.widget.imageview; import android.widget.linearlayout; import android.widget.textview; import android.widget.toast; import android.os.build; public class mainactivity extends activity implements onclicklistener { handler handler = new handler(); @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); 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(), "我来自其他线程!", toast.length_short).show(); } }); } @override public void onclick(view v) { toast toast = null; switch (v.getid()) { case r.id.btnsimpletoast: toast.maketext(getapplicationcontext(), "默认toast样式", toast.length_short).show(); break; case r.id.btnsimpletoastwithcustomposition: toast = toast.maketext(getapplicationcontext(), "自定义位置toast", toast.length_long); toast.setgravity(gravity.center, 0, 0); toast.show(); break; case r.id.btnsimpletoastwithimage: toast = toast.maketext(getapplicationcontext(), "带图片的toast", toast.length_long); toast.setgravity(gravity.center, 0, 0); linearlayout toastview = (linearlayout) toast.getview(); imageview imagecodeproject = new imageview(getapplicationcontext()); imagecodeproject.setimageresource(r.drawable.ic_launcher); toastview.addview(imagecodeproject, 0); toast.show(); break; case r.id.btncustomtoast: layoutinflater inflater = getlayoutinflater(); view layout = inflater.inflate(r.layout.custom, (viewgroup) findviewbyid(r.id.lltoast)); imageview image = (imageview) layout .findviewbyid(r.id.tvimagetoast); image.setimageresource(r.drawable.ic_launcher); textview title = (textview) layout.findviewbyid(r.id.tvtitletoast); title.settext("attention"); textview text = (textview) layout.findviewbyid(r.id.tvtexttoast); text.settext("完全自定义toast"); toast = new toast(getapplicationcontext()); toast.setgravity(gravity.right | gravity.top, 12, 40); toast.setduration(toast.length_long); toast.setview(layout); toast.show(); break; case r.id.btnruntoastfromotherthread: new thread(new runnable() { public void run() { showtoast(); } }).start(); break; } } }
运行即可。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。