Android Toast通知用法实例详解_Android

本文实例讲述了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
, 通知
toast
android中toast的用法、android toast用法、android的toast用法、yii分页用法实例详解、android创意实例详解,以便于您获取更多的相关知识。

时间: 2024-08-31 12:44:09

Android Toast通知用法实例详解_Android的相关文章

Android Toast通知用法实例详解

本文实例讲述了Android Toast通知用法.分享给大家供大家参考,具体如下: Toast在手机屏幕上向用户显示一条信息,一段时间后信息会自动消失. 1.默认用法 复制代码 代码如下:Toast.makeText(getApplicationContext(), "默认Toast样式",Toast.LENGTH_SHORT).show(); 2.Fragment中的用法 复制代码 代码如下:Toast.makeText(getActivity(),"网络连接错误,请检察网

Android Dialog对话框用法实例详解_Android

本文实例讲述了Android Dialog对话框用法.分享给大家供大家参考,具体如下: Activities提供了一种方便管理的创建.保存.回复的对话框机制,例如 onCreateDialog(int), onPrepareDialog(int, Dialog), showDialog(int),dismissDialog(int)等方法,如果使用这些方法的话,Activity将通过getOwnerActivity()方法返回该Activity管理的对话框(dialog). 1. onCreat

android之camera用法实例详解_Android

本文实例讲述了android之camera用法.分享给大家供大家参考.具体如下: 1.关于预览横竖差90度的问题 原因分析 经过查证和实验,可以证实:Android提供的SDK(android.hardware.Camera)里大概不能正常的使用竖屏(portrait layout)加载照相机,当用竖屏模式加载照相机时会产生以下情况: ①. 照相机成像左倾90度(倾斜): ②. 照相机成像长宽比例不对(失比). 之所以是"大概",原因是因为可能可以通过一些比较复杂的手段解决.如果以上成

Java中的instanceof关键字在Android中的用法实例详解_java

在下面介绍Android中如何使用instanceof关键字开发更方便时,先来温习一下java中instanceof的概念. instanceof大部分的概念是这样定义的:instanceof是Java的一个二元操作符,和==,>,<是同一类东西.由于它是由字母组成的,所以也是Java的保留关键字.它的作用是测试它左边的对象是否是它右边的类的实例,返回boolean类型的数据.举个栗子: String s = "I AM an Object!"; boolean isObj

Android控件之ListView用法实例详解_Android

本文实例讲述了Android控件之ListView用法.分享给大家供大家参考.具体如下: 示例一: 在android开发中ListView是比较常用的组件,它以列表的形式展示具体内容,并且能够根据数据的长度自适应显示. main.xml布局文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/LinearLayout01" androi

Android开发之Animations动画用法实例详解_Android

本文实例讲述了Android开发之Animations动画用法.分享给大家供大家参考,具体如下: 一.动画类型 Android的animation由四种类型组成:alpha.scale.translate.rotate XML配置文件中 alpha 渐变透明度动画效果 scale 渐变尺寸伸缩动画效果 translate 画面转换位置移动动画效果 rotate 画面转移旋转动画效果 Java Code代码中 AlphaAnimation 渐变透明度动画效果 ScaleAnimation 渐变尺寸

Android开发之TimePicker控件用法实例详解_Android

本文实例分析了Android开发之TimePicker控件用法.分享给大家供大家参考,具体如下: 新建项目: New Android Project-> Project name:HelloSpinner Build Target:Android 2.2 Application name:HelloSpinner Package name:com.b510 Create Activity:MainActivity Min SDK Version:9 Finish 运行效果: 如果: return

Android开发之TabActivity用法实例详解_Android

本文实例讲述了Android开发之TabActivity用法.分享给大家供大家参考,具体如下: 一.简介 TabActivity继承自Activity,目的是让同一界面容纳更多的内容.TabActivity实现标签页的功能,通过导航栏对各个页面进行管理. 二.XML布局文件 注意: 1.TabActivity的布局文件要求以TabHost作为XML布局文件的根. 2.通常我们采用线性布局,所以<TabHost> 的子元素是 <LinearLayout>. 3.<TabWidg

Android编程之SurfaceView实例详解_Android

本文实例讲述了Android编程之SurfaceView用法.分享给大家供大家参考,具体如下: 关于surfaceView相关知识: View和SurfaceView主要区别: 1. View只能在UI线程中刷新,而SurfaceView可以在子线程中刷新 2. SurfaceView可以控制刷新频率 SurfaceView几个重要的方法: 1. 继承SurfaceView 后调用getHolder()方法可以获取到mSurfaceHolder对象这个对于可以控制SurfaceView的绘制 2