您好,欢迎访问一九零五行业门户网

Android UI控件系列:Toast(提示)

toast用于向用户显示一些帮助/提示。下面我做了5中效果,来说明toast的强大,定义一个属于你自己的toast。
注意:
length_long—长时间显示视图或文本提示
length_short—短时间显示视图或文本提示
setgravity(int gravity,int xoffset,int yoffset)—设置提示应该在屏幕上的显示的位置
setduration(int duartion)—设置提示显示的持续时间
1.默认效果
代码
toast.maketext(getapplicationcontext(), "默认toast样式", toast.length_short).show();
2.自定义显示位置效果
代码
toast = toast.maketext(getapplicationcontext(), "自定义位置toast", toast.length_long); toast.setgravity(gravity.center, 0, 0); toast.show();
3.带图片效果
代码
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.icon); toastview.addview(imagecodeproject, 0); toast.show();
4.完全自定义效果
代码
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.icon); 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();
5.其他线程
代码
new thread(new runnable() { public void run() { showtoast(); } }).start();
完整代码
1.main,java
package com.wjq.toast;<br>import android.app.activity;<br>import android.os.bundle; <br>import android.os.handler;<br>import android.view.gravity;<br>import android.view.layoutinflater; <br>import android.view.view;<br>import android.view.viewgroup;<br>import android.view.view.onclicklistener; <br>import android.widget.imageview;<br>import android.widget.linearlayout;<br>import android.widget.textview; <br>import android.widget.toast;<br>public class main extends activity implements onclicklistener {<br>handler handler = new handler();<br>@override<br>public void oncreate(bundle savedinstancestate) {<br> super.oncreate(savedinstancestate);<br> setcontentview(r.layout.main);<br> findviewbyid(r.id.btnsimpletoast).setonclicklistener(this);<br> findviewbyid(r.id.btnsimpletoastwithcustomposition).setonclicklistener(<br> this);<br> findviewbyid(r.id.btnsimpletoastwithimage).setonclicklistener(this);<br> findviewbyid(r.id.btncustomtoast).setonclicklistener(this);<br> findviewbyid(r.id.btnruntoastfromotherthread).setonclicklistener(this);<br>}<br>public void showtoast() {<br> handler.post(new runnable() {<br> @override<br> public void run() {<br> toast.maketext(getapplicationcontext(), "我来自其他线程!",<br> toast.length_short).show();<br> }<br> });<br>}<br>@override <br>public void onclick(view v) {<br> toast toast = null;<br> switch (v.getid()) {<br> case r.id.btnsimpletoast:<br> toast.maketext(getapplicationcontext(), "默认toast样式",<br> toast.length_short).show();<br> break;<br> case r.id.btnsimpletoastwithcustomposition:<br> toast = toast.maketext(getapplicationcontext(),<br> "自定义位置toast", toast.length_long);<br> toast.setgravity(gravity.center, 0, 0);<br> toast.show();<br> break;<br> case r.id.btnsimpletoastwithimage:<br> 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.icon); toastview.addview(imagecodeproject, 0); toast.show();<br> break;<br> case r.id.btncustomtoast:<br> 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.icon); 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();<br> break;<br> case r.id.btnruntoastfromotherthread:<br> new thread(new runnable() { public void run() { showtoast(); } }).start();<br> break;<br> }<br>}<br>}
2.main,xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5dip" android:gravity="center"> <button android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/btnsimpletoast" android:text="默认"></button> <button android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="自定义显示位置" android:id="@+id/btnsimpletoastwithcustomposition"></button> <button android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/btnsimpletoastwithimage" android:text="带图片"></button> <button android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="完全自定义" android:id="@+id/btncustomtoast"></button> <button android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="其他线程" android:id="@+id/btnruntoastfromotherthread"></button> </linearlayout>
3.custom.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/tvimagetoast" /> <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>
以上就是android ui控件系列:toast(提示)的内容。
其它类似信息

推荐信息