第一种,常规:
toast = Toast.makeText(HelloWorld.this, "Toast默认情况", Toast.LENGTH_SHORT);
toast.show();
第二种:自定义显示位置
toast = Toast.makeText(HelloWorld.this, "自定义显示位置", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
第三种:带图片的Toast
toast = Toast.makeText(HelloWorld.this, "带图片的Toast", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
LinearLayout toastView = (LinearLayout)toast.getView();
ImageView toastImage = new ImageView(HelloWorld.this);
toastImage.setImageResource(R.drawable.android2);
toastView.addView(toastImage,0);
toast.show();
第四种:完全自定义的toast
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.other,null);
toast = new Toast(HelloWorld.this);
toast.setGravity(Gravity.CENTER,0,0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
第五种:来自其他线程的Toast
Thread t = new Thread(OtherClasInstance);
t.start();
完整源代码:
package com.ymq.hello;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
public class HelloWorld extends Activity {
private static Button b1 = null;
private static Button b2 = null;
private static Button b3 = null;
private static Button b4 = null;
private static Button b5 = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("activity has started...");
setContentView(R.layout.main);
b1 = (Button)this.findViewById(R.id.b1);
b2 = (Button)this.findViewById(R.id.b2);
b3 = (Button)this.findViewById(R.id.b3);
b4 = (Button)this.findViewById(R.id.b4);
b5 = (Button)this.findViewById(R.id.b5);
myButtonListener bl = new myButtonListener();
b1.setOnClickListener(bl);
b2.setOnClickListener(bl);
b3.setOnClickListener(bl);
b4.setOnClickListener(bl);
b5.setOnClickListener(bl);
}
class myButtonListener implements OnClickListener{
Toast toast = null;
public void onClick(View v) {
if(v.equals(b1))
{
toast = Toast.makeText(HelloWorld.this, "Toast默认情况", Toast.LENGTH_SHORT);
toast.show();
}else if(v.equals(b2)){
toast = Toast.makeText(HelloWorld.this, "自定义显示位置", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}else if(v.equals(b3)){
toast = Toast.makeText(HelloWorld.this, "带图片的Toast", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
LinearLayout toastView = (LinearLayout)toast.getView();
ImageView toastImage = new ImageView(HelloWorld.this);
toastImage.setImageResource(R.drawable.android2);
toastView.addView(toastImage,0);
toast.show();
}else if(v.equals(b4)){
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.other,null);
toast = new Toast(HelloWorld.this);
toast.setGravity(Gravity.CENTER,0,0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}else if(v.equals(b5)){
Thread t = new Thread();
t.start();
toast = Toast.makeText(HelloWorld.this, "来自其他线程的Toast", Toast.LENGTH_SHORT);
toast.show();
}
}
}
}
- 大小: 12.7 KB
- 大小: 10.5 KB
- 大小: 14.8 KB
- 大小: 14.5 KB
分享到:
相关推荐
Android Toast 自定义背景、图片 随心使用
Android Toast 工具类
主要为大家详细介绍了android自定义Toast设定显示时间,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
android Toast大全(五种情形)建立属于你自己的Toast
全局Toast工具类,作用:连续显示toast提示时取消上一个toast 通过ToastUtil.toast(Context(), "文本")调用
Android Toast 大全 (五种形式) 建立属于自己的 Toast
android Toast对象的使用 自定义Toast
Android Toast各种使用方法及DEMO
React Native Toast 一个用于android和ios类似于android toast提示控件,android使用原生toast, ios使用UIView Toast
一个简单的自定义Toast资源,您可以根据自己的需要更改我的代码即可实现您想要的效果(主要是更改xml文件的布局),这是androidstudio的项目,但是不妨碍在eclipse中使用,可以直接在eclipse中新建android项目,然后将...
最近在开发中我们经常会在适配5.0以后的机型遇到各种各样的问题,其中有一个不大不小的问题就是:Toast不显示问题,这篇文章就给大家总结了Android 5.0以上Toast不显示的原因与解决方法,有需要的朋友们可以参考借鉴...
android中Toast和Notification的应用.
c# winform 类似android toast消息功能 超级强大,多个项目在用,可插拔,封装好了,拿去就可以用了
Android Toast使用解析附代码,实现类似WEB开发中的弹出层效果,当用户点击一个链接或操作按钮时,会弹出一个操作层(浮动层)或浮动菜单,进而执行...本源码是使用Android中的Toast对象来模拟实现网页中的弹出层效果。
小米mimu系统,会对toast进行拦截,在用户提交的toast 消息体拼接一个前缀,由于该种会导致插件工程出现资源错乱,获取appLabel异常,现我们通过hook 动态代理,对消息发送做一个劫持,修改消息信息,还原原来的消息
Demo android service toast 第一个版本
当点击某个控件会弹出Toast时,如果直接使用Toast.makeText()的方式...解决这个问题很简单,Toast.makeText()会不断的实例Toast这个对象,我们只要在Toast不为空的情况下不再去实例就行了。这里是一个简单的ToastDemo。
android 解决Toast重复显示问题!