`
hulunberbus
  • 浏览: 858614 次
文章分类
社区版块
存档分类
最新评论

Android中Toast的用法简介(转)

 
阅读更多

原链接:http://www.cnblogs.com/GnagWang/archive/2010/11/26/1888762.html


Toast是Android中用来显示显示信息的一种机制,和Dialog不一样的是,Toast是没有焦点的,而且Toast显示的时间有限,过一定的时间就会自动消失。下面用一个实例来看看如何使用Toast。

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;

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.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Main 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.icon);
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.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();
break;
case R.id.btnRunToastFromOtherThread:
new Thread(new Runnable() {
public void run() {
showToast();
}
}).start();
break;

}

}
}

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>

 


作者:GangWang
出处:http://www.cnblogs.com/GnagWang/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

分享到:
评论

相关推荐

    Android Toast各种使用方法及DEMO

    Android Toast各种使用方法及DEMO

    Android_Toast用法.docx

    Android_Toast用法

    Android 5.0以上Toast不显示的解决方法

    实际上用户本意只是想关闭Notification,但是Toast的show方法中有调用INotificationManager这个类,而这个类在用户关闭消息通知权限的同时被禁用了,所以我们的吐司无法显示。 Toast.show() 效果图 自定义Toast...

    PhoneGap android的Toast插件

    PhoneGap android的Toast插件,使用方法不用介绍了,用phoneGap开发android应用的人都知道,希望对大家有帮助。

    Android学习笔记之Button,Toast,menu的简单用法

    Android学习笔记之Button,Toast,menu的简单用法

    Android Notification Toast用法演示范例.rar

    Android Notification消息框 Toast弹出框用法演示范例,本例中关于 Toast弹出框的演示,演示了适时的 Toast和长时间的 Toast,关于Notification的定义,则演示了高级Notification的用法,自定义4种Notification的...

    Android 演示简单toast和带图片toast的实现方法.rar

    Android 演示简单toast和带图片toast的实现方法,这些toast在平时的Android应用开发中使用频繁,本源码演示了两种最实用toast的用法,一种是不带图片,另一种是带图片:  // 简单的toast,不带图片的实现方法:  ...

    Android学习下 toast notification用法.rar

    一个关于安卓toast和notification使用方法的Android源码,来自eoeandroid社区,对初学者或许是个帮助吧。

    Toast用法详解(各种自定义Toast)实例

    android之Toast用法详解(各种自定义Toast)实例

    Android使用Toast显示消息提示框

    在前面的实例中,已经应用过Toast类来显示一个简单的提示框了。这次将对Toast进行详细介绍。Toast类用于在屏幕中显示一个消息提示框,该消息提示框没有任何控制按钮,并且不会获得焦点,经过一段时间后自动消失。...

    Android Toast的用法总结(五种用法)

    本篇文章主要介绍了Android Toast的用法总结(五种用法),android toast几种使用方法 toast经常会用到,今天做个总结,特别是自定义toast的布局,值得一看。

    android自定义Toast设定显示时间

    开发android的同学可能会抱怨Toast设定显示的时长无效...本文使用方法三进行实现,难度不大,直接看代码吧. package com.open.toast; import android.content.Context; import android.graphics.Color; import andro

    Android Toast用法代码实例

    摘要:Java源码,Android,Toast,Android源码 Android Toast用法代码实例,建立属于你... Toast在Android系统中用于向用户显示一些帮助/提示,本源码是一个较基本的Toast用法实例,还望通过本例能加深您对Toast的理解。

    Android Toast通知用法实例详解

    本文实例讲述了Android Toast通知用法。分享给大家供大家参考,具体如下: Toast在手机屏幕上向用户显示一条信息,一段时间后信息会自动消失。 1.默认用法 代码如下:Toast.makeText(getApplicationContext(), ...

    Android 自定义 Toast 显示时间

    Android 自定义 Toast 显示时间 实现代码: ... import android.content.... * 使用方法 * 1.先初始化类 MyToast myToast = new MyToast(this); * 2.显示消息 myToast.setText(要显示的内容); //设置要显示的内容 *

    toast几种用法

    最近学习的toast用法,包括自定义位置toast和带图片的toast

    Android Toast自定义显示时间

    常规使用方法这里不做说明,继前一篇博客《Android中Toast全屏显示》 ,其中抛砖引玉的给出一个简单的实现Toast全屏显示的方法后,发现无法控制Toast的显示时长。虽然Toast中有setDuration(int duration)接口,但是...

    自定义Toast的显示内容和显示位置

    Android中定义了一个Toast对象,用以弹出一个窗口来给予用户帮助和提示,和对话框不同的是,Toast并不是以独占方式显示的,它并不会抢夺用户的焦点,在弹出Toast的时候,依然可以对之前的界面进行操作,我们在“”...

    Android中使用Toast.cancel()方法优化toast内容显示的解决方法

    看到Toast有一个cancel()方法: 代码如下:void cancel() Close the view if it’s showing, or don’t show it if it isn’t showing yet. 做程序员的,基本一看api就知道,用这个可以取消上一个toast的显示,然后...

    android之自定义Toast使用方法

    使用自定义Toast,首先我们需要添加一个布局文件,该布局文件的结构和Activity使用的布局文件结构一致,在该布局文件中我们需设计我们Toast的布局,例如: 代码如下: &lt;?xml version=”1.0″ encoding=”utf-8″?&...

Global site tag (gtag.js) - Google Analytics