Toast: http://developer.android.com/reference/android/widget/Toast.html 

  在部分手机中如果当把编辑完的消息发送完成之后,通常在手机下方会出现:”消息发送成功“的提示消息或相类似的提示消息,并且通常会短暂的停留之后会自动消失;那么这个就是一个典型的Toast应用;

  消息提示框的种类有多种比如说:Dialog(在以后章节中会介绍)。而Toast与Dialog不同:Dialog是以独占的方式显示的,换句话说就是:如果不关闭它Dialog会一直显示于当前界面,而Toast则会短暂的停留之后自动关闭;

  实现Toast其实很简单,在这里介绍三种方式:

  1. 直接使用静态方法:makeText(Context, int/CharSequence,int);
    1. Toast toast = Toast.makeText(UsingIntent.this, "Some thing you want to show", Toast.LENGTH_LONG);
      toast.show();

    调用makeText方法,会以文本的方式来进行显示,因为它默认载入一个TextView,而第三参数可以通过:Toast的二个静态域LENGTH_LONG/LENGTH_SHORT来进行设置; 之所以返回一个Toast是为了方便扩展,比如调用setDuration方法;

  2.  添加一些自己想要的效果,个人觉得有点像重写makeText方法的味道

  

// LayoutInflater inflater = (LayoutInflater) YouAcvity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); A
                                     View view = getLayoutInflater().inflate(your_layout_file,null); //B :A,B二者中选其一即可,在这里只是提供二种途径
                                     Toast toast = new Toast(YouActivity.this);
                                     toast.setDuration(Toast.LENGTH_LONG);
                                     toast.setView(view);
                                     toast.show(); //别忘记invoke show方法

  3.   Toast消息提示框会在一定时间后自动消失,如果想永远停留该怎么办呢?能不能做得到呢?

      首先告诉你永远停留是可以做得到的,可怎么去实现呢?

      查看源码后获知,Toast.show方法是采用队列来进行显示,系统会依次取出一个Toast并显示,等到一定的时间后它会自动消失,系统再会取出下一Toast,直接在整个队列中没有Toast为止;在Toast类有个私有的静态内部类叫作:TN;因为TN是私有,那么该如何访问呢? 答案是反射。

 1 try {
 2  Field field =toast.getClass().getDeclaredField("mTN");                             
field.setAccessible(true); Object object = field.get(toast); 3 Method method=object.getClass().getDeclaredMethod("show",null);
5 //object 最后定义在类里面,这样方面多次使用,比如关闭Toast
4 method.invoke(object,new Object[0]); 6 //new Object[0]可以用null代替,但是因为是可变参数,所以最好使用new Object[0]
7
} catch (Exception e) 8 { 9 e.printStackTrace(); 10 }

  如果有关闭Toast,可以使用(//method =object.getClass().getDeclaredMethod("hide",null););同时Toast提供一个其它的方法比如:setGravity方法;    

Notification:  http://developer.android.com/reference/android/app/Notification.html

  • 理论基础:
    •   状态条与状态栏:用图说明可能会更清楚,请见下图!
      • 状态条:Android开发之Toast and Notification
      • 状态栏:Android开发之Toast and Notification,需要执行下拉动作,才可以查看整个状态栏具体信息。
      • 关系:状态栏包含状态条!
    • Notification 有这些组件:
      • 内容标题
      • 大图标 : 通常显示在左侧
      • 内容文本
      • Content info: 译为备注信息应该会好点,Anyway ,you can believe your choice.
      • 小图标
      • 时间戳
  • 如何运用Notification
    • 获取NotificationManager对象
    • 获得Notification对象
    • 设置Notification属性
    • 设置PendingIntent
    • Invoke notify方法

总体分这五步!!!

Notification设计哲学是:管理通知对象,通知实体对象,Build对象(内部类),以及PendingIntent!

管理通知对象:NotificationManager: 拥有发布,更新,取消等通知功能。

通知实体对象:拥有多个实体属性

Build对象:用于创建通知实体对象以及设置相关属性

PendingIntent: 如果要启动新的Activity通常是调用startActivity,startActivityForResult;而PendingIntent是对Intent的进一步封装,以便当我们查看Notification执行相应动作时转入对应的Activity.

在Andorid 3.0以下创建通知对象通常是直接利用:Notification的构建函数进行,而伴随的Android os的更新升级,更多时候是使用另外二个支持类(内部类):NotificationCompat.Builder,Notification.Builder 这二者功能类似,主要区别在于:NotificationCompat.Builder可以兼容API level 4,并且它是由:SupportLibrary提供。

实码演练:这时提供一个小Demo。

Layout file: 

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical" android:layout_width="fill_parent"
 4     android:layout_height="fill_parent">
 5 
 6     <Button
 7         android:id="@+id/notification_demo1"
 8         android:layout_width="match_parent"
 9         android:layout_height="wrap_content"
10         android:text="@string/notification_btn_string1"
11         />
12     <Button
13         android:id="@+id/notification_demo2"
14         android:layout_width="match_parent"
15         android:layout_height="wrap_content"
16         android:text="@string/notification_btn_string2"
17         />
18     <Button
19         android:id="@+id/notification_demo2_1"
20         android:layout_width="match_parent"
21         android:layout_height="wrap_content"
22         android:text="@string/notification_btn_string2_1"
23         />
24     <Button
25         android:id="@+id/notification_demo3"
26         android:layout_width="match_parent"
27         android:layout_height="wrap_content"
28         android:text="@string/notification_btn_string3"
29         />
30     <Button
31         android:id="@+id/notification_demo4"
32         android:layout_width="match_parent"
33         android:layout_height="wrap_content"
34         android:text="@string/notification_btn_string4"
35         />
36     <Button
37         android:id="@+id/notification_demo5"
38         android:layout_width="match_parent"
39         android:layout_height="wrap_content"
40         android:text="@string/notification_btn_string5"
41         />
42 </LinearLayout>
View Code

相关文章:

  • 2021-08-26
  • 2021-10-26
  • 2021-09-11
  • 2021-09-19
  • 2021-10-21
  • 2022-12-23
猜你喜欢
  • 2021-06-28
  • 2022-02-18
  • 2022-12-23
  • 2021-06-25
  • 2021-09-26
  • 2022-02-22
  • 2022-12-23
相关资源
相似解决方案