`

Toast的多种样式(附带Notification)

阅读更多
Toast以前用的时候一直以为只有文字提示,偶然得知也有多种样式,研究了一下,贴出来供大家参考一下.
Toast.makeText(this, "", Toast.LENGTH_LONG).show();

这是大家最经常用的提示了.
下面给大家上例外几种,具体作用都贴在代码注释中

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/but1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="显示图片Toast" />
    
    <Button
        android:id="@+id/but2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="图片加文字toast" />
    
    <Button
        android:id="@+id/but3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="动态图toast的gif" />
    
    <Button
        android:id="@+id/butnoti"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="显示通知" />
    
</LinearLayout>


package com.example.mytoast;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat.Builder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.gif.MyGifView;

public class MainActivity extends Activity implements OnClickListener{
	
	//标记Notification的ID,这样子可以反复修改这一个通知,而不会生成新的通知
	public static final int NOTIFICATION  = 1200;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		findViewById(R.id.but1).setOnClickListener(this);
		findViewById(R.id.but2).setOnClickListener(this);
		findViewById(R.id.but3).setOnClickListener(this);
		findViewById(R.id.butnoti).setOnClickListener(this);
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.but1:
			//图片toast
			Toast imagetost = Toast.makeText(this, "", Toast.LENGTH_LONG);
			ImageView imageView = new ImageView(this);
			imageView.setImageResource(R.drawable.pkqg);
			imagetost.setView(imageView);
			imagetost.show();
			break;
		case R.id.but2:
			//自定义布局的toast
			Toast imagetost1 = Toast.makeText(this, "", Toast.LENGTH_LONG);
			View view = getLayoutInflater().inflate(R.layout
					.toastimageandtext_layout, null);
			imagetost1.setView(view);
			imagetost1.show();
			break;
		case R.id.but3:
			//动态图的toast,需要自己写一个MyGifView的动态图
			//android里面的ImageView只支持静态图,如果要显示Gif的动态图的话,要自己去画
			Toast imagetost2 = Toast.makeText(this, "", Toast.LENGTH_LONG);
			View view2 = getLayoutInflater().inflate(R.layout
					.toast_layout, null);
			MyGifView mygifview1 = (MyGifView) view2.findViewById(R.id.imagegif); 
			mygifview1.setImageSouce(R.drawable.pkqg);
			imagetost2.setView(view2);
			imagetost2.show();
			break;
		case R.id.butnoti:
			//附带一个Notification,这种Notification有很多参数,想要研究的话可以去参考一下官网
			Builder builder = new Builder(this);
			builder.setSmallIcon(R.drawable.pkq);
			builder.setContentTitle("您有一个新消息");
			builder.setContentText("今天可以随机领取天空套哦!");
			Notification notification = builder.build();
			NotificationManager manager = (NotificationManager) 
					getSystemService(Context.NOTIFICATION_SERVICE);
			manager.notify(NOTIFICATION, notification);
			break;
		default:
			break;
		}
	}

}


MyGifView.Java

package com.example.gif;

import com.example.mytoast.R;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Movie;
import android.util.AttributeSet;
import android.view.View;

/**
 * 自定义View 播放gif动画 
 * @author cuiran
 * @version 1.0.0
 */
public class MyGifView extends View {

	private long movieStart;
	
	private Movie movie = Movie.decodeStream(getResources().openRawResource(R.drawable.pkq));
	
	public MyGifView(Context context) {
		this(context,null);
	}
	public MyGifView(Context context, AttributeSet attrs)
	{
		super(context,attrs);
	}

	private MyGifView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
	}
	
	//设置图片的资源
	public void setImageSouce(int id){
		movie = Movie.decodeStream(getResources().openRawResource(id));
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
			long curTime=android.os.SystemClock.uptimeMillis();
		//第一次播放
		if (movieStart == 0) {
		movieStart = curTime;
		}
		if (movie != null) {
			int duraction = movie.duration();
			int relTime = (int) ((curTime-movieStart)%duraction);
			movie.setTime(relTime);
			movie.draw(canvas, 200, 350);
			//强制重绘
			invalidate();
		}
		super.onDraw(canvas);
		
	}

}


toast_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

     <com.example.gif.MyGifView
        android:id="@+id/imagegif"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    
</LinearLayout>


toastimageandtext_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/imagetoast"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/pkqg" />
        
    <TextView
        android:id="@+id/but2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="皮卡丘正在愉快的奔跑" />
    
</LinearLayout>
分享到:
评论

相关推荐

    Android 源码:toast_and_notification

    Android 源码:toast_and_notification,100ms延迟后,振动250ms,停止100ms后振动500ms,没有搞懂具体是干什么用的,本人非专业人士,发在源码爱好者,供喜欢的人研究。

    toast_and_notification.zip

    在Android开发中,Toast和Notification是两种非常重要的用户反馈机制,它们用于向用户显示短暂的信息提示。本项目“toast_and_notification.zip”似乎包含了与这两者相关的代码实现,特别是结合了振动效果,这在某些...

    android学习之toast和notification

    `Notification`具有更丰富的显示样式,如包含标题、文本、图标、操作按钮等,并且可以在通知中心查看历史记录。创建`Notification`的基本步骤: 1. 创建`NotificationCompat.Builder`对象:`new NotificationCompat....

    Android中Toast和Notification的应用.

    在Android应用开发中,`Toast`和`Notification`是两种重要的用户反馈机制,它们用于向用户展示临时或持久的信息。让我们深入探讨这两种机制的工作原理、使用场景和实现方法。 首先,`Toast`是一种轻量级的提示方式...

    Android学习下 toast notification用法.rar

    在Android开发中,Toast和Notification是两种非常重要的用户交互组件,它们用于向用户显示短暂的信息提示。本资源“Android学习下 toast notification用法.rar”包含了有关如何在Android应用中使用这两种组件的源码...

    PhoneGap Android插件 调用toast 和 notification

    在本例中,我们关注的是如何在PhoneGap应用中调用Android的`Toast`和`Notification`功能。 `Toast`是Android系统提供的一种轻量级提示方式,它可以显示简短的信息,并在一段时间后自动消失。`Notification`则更为...

    Android-Android实现Toast自定义样式

    默认情况下,`Toast`的样式和位置都是系统固定的,但开发者可以根据需求自定义其样式和显示位置,以增强用户体验。本文将详细介绍如何在Android中实现`Toast`的自定义样式,包括自定义位置、添加图片以及自定义显示...

    vue-toast-notification:Vue.js的另一个Toast Notification插件

    import VueToast from 'vue-toast-notification' ; // Import one of the available themes //import 'vue-toast-notification/dist/theme-default.css'; import 'vue-toast-notification/dist/theme-sugar.css' ; ...

    Android入门开发实例--Toast、Notification、Intent应用

    在Android应用开发中,`Toast`、`Notification`和`Intent`是三个核心组件,它们在应用程序中扮演着至关重要的角色。本篇文章将深入探讨这三个概念,并通过实例讲解如何在实际开发中运用它们。 首先,我们来理解`...

    友好地互动交流提示信息Toast、notification

    `Toast`和`Notification`是两种常用的方式来实现这一目标。本文将详细介绍这两种技术的基本用法及其在实际开发中的应用。 `Toast`是Android系统提供的一种轻量级提示方式,它会短暂地显示在屏幕的某个位置,用于向...

    notification_Toast_android_

    在Android应用开发中,`Notification`和`Toast`是两种重要的用户反馈机制,它们用于向用户展示短暂的信息提示。下面将详细介绍这两种机制的使用方法及其在Android开发中的应用。 `Toast`是Android系统提供的一种轻...

    Android-AndroidToast即便关闭了通知权限也会正常显示

    在Android系统中,`Toast`和`Notification`虽然都是用来与用户交互的方式,但它们属于不同的通道。通知权限是针对`Notification`的,允许应用在通知栏展示通知。而`Toast`则不需要这个权限,因为它并不占用通知栏...

    android toast and notification完整代码

    在Android开发中,Toast和Notification是两种非常重要的用户反馈机制,它们用于向用户显示简短的信息或提醒。本文将深入探讨这两个概念,并提供完整的代码示例。 **1. Android Toast** Toast是Android系统中一种...

    Android Notification Toast用法演示范例.rar

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

    toast_and_notification

    在Android开发中,Toast和Notification是两种非常重要的用户反馈机制,它们用于向用户显示短暂的信息提示,而无需中断当前的应用程序流程。让我们深入探讨这两种机制以及如何在Android源代码中实现它们。 首先,`...

    Android的各种通知Notification、Dialog、Toast、Snackbar

    通知(Notification)、对话框(Dialog)、吐司(Toast)和Snackbar是Android系统提供的一些关键组件,用于向用户展示各种信息和提示。下面将详细介绍这些组件的功能、用法以及它们在不同场景下的适用性。 **1. ...

    移动端toast弹框样式美观轻量支持vue20

    标题“移动端`toast`弹框样式美观轻量支持`vue2.0`”指出,这个组件专为Vue.js 2.0版本打造,旨在提供一个既好看又不占用过多资源的`Toast`解决方案。`Toast`通常用于显示非阻塞式信息,不会打断用户的操作流程,但...

    android默认Toast,各种自定义Toast

    自定义Toast样式主要是通过修改Toast的布局来实现的。首先,我们需要创建一个XML布局文件,定义我们想要的样式,例如: ```xml &lt;!-- res/layout/custom_toast.xml --&gt; android:layout_width="wrap_content" ...

Global site tag (gtag.js) - Google Analytics