`
zkh43javaeye
  • 浏览: 84374 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

转 【通知 Toast详细用法(显示view)】 【android Toast大全(五种情形)建立属于你自己的Toast】

阅读更多

 

原文地址: http://www.pocketdigi.com/20100904/87.html

 

今天学习Android通知 Toast的用法,Toast在手机屏幕上向用户显示一条信息,一段时间后信息会自动消失。信息可以是简单的文本,也可以是复杂的图片及其他内容(显示一个view)。
看效果图:

今天演示的有两种用法,如上图
main.xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Button android:id="@+id/button1"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="Toast显示View"
/>
<Button android:id="@+id/button2"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="Toast直接输出"
/>
</LinearLayout>

两个按钮,很简单
程序代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.pocketdgig.toast;
 
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
 
public class main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button1=(Button)findViewById(R.id.button1);
        button1.setOnClickListener(bt1lis);
        Button button2=(Button)findViewById(R.id.button2);
        button2.setOnClickListener(bt2lis);
    }
    OnClickListener bt1lis=new OnClickListener(){
 
		@Override
		public void onClick(View v) {
			showToast();
		}
 
    };
    OnClickListener bt2lis=new OnClickListener(){
		@Override
		public void onClick(View v) {
			Toast.makeText(main.this,"直接输出测试", Toast.LENGTH_LONG).show();
		}
 
    };
    public void showToast(){
    	LayoutInflater li=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         
//LayoutInflater li= getLayoutInflater(); 这个2种方法都可以起效;

    	View view=li.inflate(R.layout.toast,null);
    	//把布局文件toast.xml转换成一个view
    	Toast toast=new Toast(this);
    	toast.setView(view);
    	//载入view,即显示toast.xml的内容
    	TextView tv=(TextView)view.findViewById(R.id.tv1);
    	tv.setText("Toast显示View内容");
    	//修改TextView里的内容
    	toast.setDuration(Toast.LENGTH_SHORT);
    	//设置显示时间,长时间Toast.LENGTH_LONG,短时间为Toast.LENGTH_SHORT,不可以自己编辑
    	toast.show();
    }
}

下面是toast.xml的内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<ImageView android:src="@drawable/toast"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
/>
<TextView android:id="@+id/tv1"
	android:text=""
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
/>
</LinearLayout>

 

android Toast大全(五种情形)建立属于你自己的Toast

原文地址: http://www.cnblogs.com/salam/archive/2010/11/10/1873654.html

 

 Toast用于向用户显示一些帮助/提示。下面我做了5中效果,来说明Toast的强大,定义一个属于你自己的Toast。

1.默认效果

代码

Toast.makeText(getApplicationContext(), "默认Toast样式",
     Toast.LENGTH_SHORT).show();

 

2.自定义显示位置效果

代码

toast = Toast.makeText(getApplicationContext(),
     "自定义位置Toast", Toast.LENGTH_LONG);
   toast.setGravity(Gravity.CENTER, 0, 0);//设置toast位子
   toast.show();

 

3.带图片效果

 

代码

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

 

4.完全自定义效果

代码:

 //使用 LayoutInflater 关联布局配置文件获取View.

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

   //获取并设置title内容;
   TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);
   title.setText("Attention");

    //获取并设置text内容;
   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();

 

5.其他线程

 代码

new Thread(new Runnable() {
    public void run() {
     showToast();
    }
   }).start();

 

 

完整代码

1.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;

  }

 }
}

 

2.main,xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent" android:padding="5dip" android:gravity="center">
 <Button android:layout_height="wrap_content"
  android:layout_width="fill_parent" android:id="@+id/btnSimpleToast"
  android:text="默认"></Button>
 <Button android:layout_height="wrap_content"
  android:layout_width="fill_parent" android:text="自定义显示位置"
  android:id="@+id/btnSimpleToastWithCustomPosition"></Button>
 <Button android:layout_height="wrap_content"
  android:layout_width="fill_parent" android:id="@+id/btnSimpleToastWithImage"
  android:text="带图片"></Button>
 <Button android:layout_height="wrap_content"
  android:layout_width="fill_parent" android:text="完全自定义"
  android:id="@+id/btnCustomToast"></Button>
 <Button android:layout_height="wrap_content"
  android:layout_width="fill_parent" android:text="其他线程"
  android:id="@+id/btnRunToastFromOtherThread"></Button>

</LinearLayout>

 

3.custom.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_height="wrap_content" android:layout_width="wrap_content"
 android:background="#ffffffff" android:orientation="vertical"
 android:id="@+id/llToast" >
 <TextView
  android:layout_height="wrap_content"
  android:layout_margin="1dip"
  android:textColor="#ffffffff"
  android:layout_width="fill_parent"
  android:gravity="center"
  android:background="#bb000000"
  android:id="@+id/tvTitleToast" />
 <LinearLayout
  android:layout_height="wrap_content"
  android:orientation="vertical"
  android:id="@+id/llToastContent"
  android:layout_marginLeft="1dip"
  android:layout_marginRight="1dip"
  android:layout_marginBottom="1dip"
  android:layout_width="wrap_content"
  android:padding="15dip"
  android:background="#44000000" >
  <ImageView
   android:layout_height="wrap_content"
   android:layout_gravity="center"
   android:layout_width="wrap_content"
   android:id="@+id/tvImageToast" />
  <TextView
   android:layout_height="wrap_content"
   android:paddingRight="10dip"
   android:paddingLeft="10dip"
   android:layout_width="wrap_content"
   android:gravity="center"
   android:textColor="#ff000000"
   android:id="@+id/tvTextToast" />
 </LinearLayout>
</LinearLayout>

 

分享到:
评论

相关推荐

    Android 5.0以上Toast不显示的解决方法

    实际上用户本意只是想关闭Notification,但是Toast的show方法中有调用INotificationManager这个类,而这个类在用户关闭消息通知权限的同时被禁用了,所以我们的吐司无法显示。 Toast.show() 效果图 自定义Toast...

    Android 演示简单toast和带图片toast的实现方法.rar

    Android 演示简单toast和带图片toast的实现方法,这些toast在平时的Android应用开发中使用频繁,本源码演示了两种最实用toast的用法,一种是不带图片,另一种是带图片:  // 简单的toast,不带图片的实现方法:  ...

    Toast的几行代码

    简单的toast效果,几行代码,没啥的。

    一个简单的带图村的Android Toast实例.rar

    一个简单的带图村的Android Toast实例,轻触按钮,即可显示出Toast提示框信息,跟随以下代码你将实现这种既简洁又漂亮实用的Toast:  ImageView iv = new ImageView(Sample_6_9.this);//创建ImageView  iv....

    Android代码-替代Toast的MessageBar

    An Android Toast replacement, similar to the one seen in the GMail app. Multiple messages can be posted in succession, and each message will be shown for 5 seconds. Usage There's two ways to use the ...

    android 图文可视化提醒 Toast与LinearLayout View

    android 图文可视化提醒 Toast与LinearLayout View

    android自定义Toast设定显示时间

    开发android的同学可能会抱怨Toast设定显示的时长无效,只能是Toast.LENGTH_LONG 或者Toast.LENGTH_SHORT 之一,为了解决这些办法,有多种实现方式: 1.使用定时器,定时调用show()方法. 2.使用CountDownTimer类,也...

    ios demo,toast view,可以指定在屏幕的中心显示或底部显示

    可以指定在屏幕的中心显示或底部显示

    带图片的Toast消息提示&单选列表项对话框&自定义View对话框

    带图片的Toast消息提示&单选列表项对话框&自定义View对话框

    Android中使用Toast.cancel()方法优化toast内容显示的解决方法

    看到Toast有一个cancel()方法: 代码如下:void cancel() Close the view if it’s showing, or don’t show it if it isn’t showing yet. 做程序员的,基本一看api就知道,用这个可以取消上一个toast的显示,然后...

    自定义对话框LoadingDialog和Toast

    自定义对话框LoadingDialog和Toast,可以添加Gif动画的Dialog,喜欢的自行下载!

    Android 自定义 Toast 显示时间

    Android 自定义 Toast 显示时间 实现代码: package com.wm.realname.util; import android.content.Context; import android.os.Handler; import android.view.View; import android.widget.Toast; /** * Toast...

    Android实用的Toast工具类封装

    大家好,Toast这个提示框大家都晓得,显示一段时间后自动消失,不能获得焦点。但是在使用中有些问题: 1)需要弹出一个新的Toast时,上一个Toast还没有显示完 2)可能重复弹出相同的信息 3)Toast具体有哪些用法不是很...

    Android代码-将 Toast 像 Tip 一样使用

    Android Tooltip Create Toast like tooltips, physical targets can be specified, or even points on screen. Many additional features and customizations. Just look at the samples Activities. ...

    Android控件系列之Toast使用介绍

    Toast英文含义是吐司,在Android中,它就像烘烤机里做好的吐司弹出来,并持续一小段时间后慢慢消失Toast也是一个容器,可以包含各种View,并承载着它们显示。 使用场景: 1、需要提示用户,但又不需要用户点击...

    Android编程实现Toast自定义布局简单示例

    本文实例讲述了Android编程实现Toast自定义布局的方法。分享给大家供大家参考,具体如下: 不知道各位客官是不是觉得系统的toast的信息很难看呢,默认的但黑色背景,毫无色彩。 那么接下来我就教大家用最简单的方式...

    XToast-超级 Toast.zip

    }框架亮点(原生 Toast 无法实现的功能)支持自定义 Toast 动画样式支持自定义 Toast 显示时长支持监听 Toast 的显示和销毁支持监听 Toast 中点击事件支持一键开启 Toast 拖拽功能支持 Toast 全局显示(需要权限)...

    一个可以替代Toast的简单MessageView

    NULL 博文链接:https://gundumw100.iteye.com/blog/2005696

    Android代码-MyToast

    Custom Toast on Android Image Result Usage MyToast.show(this, "MyToast Sample", true); Create toast_layout.xml : MyToast.java : package com.mikhaellopez.mytoast; import android.content....

    吐司工具类(Toast)

    本工具类通过Toast对象实现即时消息功能(即随着你传入的message而变化),同时也实现了自定义弹出的位置、以及自定义弹出View的功能!

Global site tag (gtag.js) - Google Analytics