Android Toast通知用法实例详解

 更新时间:2016年07月13日 15:46:34   作者:dreamzml  
这篇文章主要介绍了Android Toast通知用法,集合实例形式详细分析了Toast通知的基本使用方法与常见显示布局效果实现技巧,需要的朋友可以参考下

本文实例讲述了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程序设计有所帮助。

相关文章

  • Android利用RecyclerView编写聊天界面

    Android利用RecyclerView编写聊天界面

    这篇文章主要为大家详细介绍了Android利用RecyclerView编写聊天界面,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-07-07
  • Android下Activity全屏显示实现方法

    Android下Activity全屏显示实现方法

    这篇文章主要介绍了Android下Activity全屏显示实现方法,以两种不同的方法来实现这一技巧,非常具有实用性,需要的朋友可以参考下
    2014-10-10
  • android实现图片闪烁动画效果的两种实现方式(实用性高)

    android实现图片闪烁动画效果的两种实现方式(实用性高)

    本文通过两种方法给大家讲解了android实现图片闪烁动画效果,实用性非常高,对这两种方法感兴趣的朋友一起通过本文学习吧
    2016-09-09
  • Android_UI 仿QQ侧滑菜单效果的实现

    Android_UI 仿QQ侧滑菜单效果的实现

    相信大家对QQ侧滑菜单的效果已经不陌生了吧,侧滑进入个人头像一侧,进行对头像的更改,我的收藏,QQ钱包,我的文件等一系列的操作,下面小编给大家分享Android_UI 仿QQ侧滑菜单效果的实现,一起看看吧
    2017-04-04
  • Android实现文件存储案例

    Android实现文件存储案例

    这篇文章主要为大家详细介绍了Android实现文件存储案例,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-11-11
  • Android中利用App实现消息推送机制的代码

    Android中利用App实现消息推送机制的代码

    Android中利用App实现消息推送机制的代码,需要的朋友可以参考下。
    2011-05-05
  • Android编程之动态壁纸实例分析

    Android编程之动态壁纸实例分析

    这篇文章主要介绍了Android编程之动态壁纸实现方法,以实例形式分析了Android动态壁纸的原理与实现步骤,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-12-12
  • Android中TextView动态设置缩进距离的方法

    Android中TextView动态设置缩进距离的方法

    项目需求如果在项目中第一行文字需要添加布局的情况我们应该怎么做呢,经过一番考虑和查找我最终选择了缩进的方式解决这个问题,这篇文章主要给大家介绍了关于Android中TextView动态设置缩进距离的相关资料,需要的朋友可以参考下
    2022-04-04
  • Android 实现页面跳转

    Android 实现页面跳转

    android使用Intent来实现页面跳转,接下来通过本文给大家分享Android 实现页面跳转的实例代码,需要的的朋友参考下吧
    2017-09-09
  • Android程序开发之UIScrollerView里有两个tableView

    Android程序开发之UIScrollerView里有两个tableView

    这篇文章主要介绍了UIScrollerView里有两个tableView 的相关资料,需要的朋友可以参考下
    2016-04-04

最新评论