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>