欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android Toast通知用法实例详解

程序员文章站 2024-03-05 17:36:25
本文实例讲述了android toast通知用法。分享给大家供大家参考,具体如下: toast在手机屏幕上向用户显示一条信息,一段时间后信息会自动消失。 1.默认用法...

本文实例讲述了android toast通知用法。分享给大家供大家参考,具体如下:

toast在手机屏幕上向用户显示一条信息,一段时间后信息会自动消失。

1.默认用法

复制代码 代码如下:
toast.maketext(getapplicationcontext(), "默认toast样式",toast.length_short).show();

2.fragment中的用法

复制代码 代码如下:
toast.maketext(getactivity(),"网络连接错误,请检察网络设置", toast.length_long).show();

3.自定义显示位置效果

toast = toast.maketext(getapplicationcontext(), "自定义位置toast", toast.length_long);
toast.setgravity(gravity.center, 0, 0);
toast.show();

4.带图片效果

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();

5.完全自定义效果

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();

6.其他线程

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;
    }
  }
}

main.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:gravity="center"
  android:orientation="vertical"
  android:padding="5dip" >
  <button
    android:id="@+id/btnsimpletoast"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="默认" >
  </button>
  <button
    android:id="@+id/btnsimpletoastwithcustomposition"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="自定义显示位置" >
  </button>
  <button
    android:id="@+id/btnsimpletoastwithimage"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="带图片" >
  </button>
  <button
    android:id="@+id/btncustomtoast"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="完全自定义" >
  </button>
  <button
    android:id="@+id/btnruntoastfromotherthread"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="其他线程" >
  </button>
</linearlayout>

custom.xml:

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/lltoast"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="#ffffffff"
  android:orientation="vertical" >
  <textview
    android:id="@+id/tvtitletoast"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="1dip"
    android:background="#bb000000"
    android:gravity="center"
    android:textcolor="#ffffffff" />
  <linearlayout
    android:id="@+id/lltoastcontent"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginbottom="1dip"
    android:layout_marginleft="1dip"
    android:layout_marginright="1dip"
    android:background="#44000000"
    android:orientation="vertical"
    android:padding="15dip" >
    <imageview
      android:id="@+id/tvimagetoast"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center" />
    <textview
      android:id="@+id/tvtexttoast"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:gravity="center"
      android:paddingleft="10dip"
      android:paddingright="10dip"
      android:textcolor="#ff000000" />
  </linearlayout>
</linearlayout>

更多关于android相关内容感兴趣的读者可查看本站专题:《android编程之activity操作技巧总结》、《android资源操作技巧汇总》、《android文件操作技巧汇总》、《android操作sqlite数据库技巧总结》、《android操作json格式数据技巧总结》、《android数据库操作技巧总结》、《android编程开发之sd卡操作方法汇总》、《android开发入门与进阶教程》、《android视图view技巧总结》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。