1. Toast

  学习创建长短不一的Toast提示,并自定义Toast在屏幕上的位置以及Toast的外观。

 1 package com.example.toastdemo;
2
3 import android.app.Activity;
4 import android.app.ActionBar;
5 import android.app.Fragment;
6 import android.os.Bundle;
7 import android.view.Gravity;
8 import android.view.LayoutInflater;
9 import android.view.Menu;
10 import android.view.MenuItem;
11 import android.view.View;
12 import android.view.ViewGroup;
13 import android.widget.Button;
14 import android.widget.ImageView;
15 import android.widget.Toast;
16 import android.os.Build;
17
18 public class MainActivity extends Activity {
19
20 private Button btnShowToastShort, btnShowToastLong, btnShowToastImage;
21
22 @Override
23 protected void onCreate(Bundle savedInstanceState) {
24 super.onCreate(savedInstanceState);
25 setContentView(R.layout.activity_main);
26
27 btnShowToastShort = (Button) findViewById(R.id.btnShowToast);
28 btnShowToastLong = (Button) findViewById(R.id.btnShowToastLong);
29 btnShowToastImage = (Button) findViewById(R.id.btnShowToastImage);
30
31 btnShowToastShort.setOnClickListener(new View.OnClickListener() {
32
33 @Override
34 public void onClick(View v) {
35 // 可以改变Toast显示的位置;
36
37 Toast shortToast = Toast.makeText(MainActivity.this,
38 "显示一个简短的Toast", Toast.LENGTH_SHORT);
39 shortToast.setGravity(Gravity.CENTER, 0, 0);
40 shortToast.show();
41 }
42 });
43
44 btnShowToastLong.setOnClickListener(new View.OnClickListener() {
45
46 @Override
47 public void onClick(View v) {
48 Toast.makeText(MainActivity.this, "显示一个较长的Toast",
49 Toast.LENGTH_LONG).show();
50 }
51 });
52
53 btnShowToastImage.setOnClickListener(new View.OnClickListener() {
54
55 // 显示图片之后,就不会显示文字。
56 // 若想显示其它,需要定义Layout
57 @Override
58 public void onClick(View v) {
59 Toast imageToast = Toast.makeText(MainActivity.this,
60 "显示一个图片的Toast", Toast.LENGTH_LONG);
61 ImageView imageView = new ImageView(MainActivity.this);
62 imageView.setImageResource(R.drawable.ic_launcher);
63 imageToast.setView(imageView);
64 imageToast.show();
65 }
66 });
67
68 }
69
70 }

MainActivity

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 xmlns:tools="http://schemas.android.com/tools"
3 android:id="@+id/container"
4 android:layout_width="match_parent"
5 android:layout_height="match_parent"
6 android:orientation="vertical"
7 tools:context="com.example.toastdemo.MainActivity"
8 tools:ignore="MergeRootFrame" >
9
10 <Button
11 android:id="@+id/btnShowToast"
12 android:layout_width="fill_parent"
13 android:layout_height="wrap_content"
14 android:text="显示一个简短的Toast" />
15
16 <Button
17 android:id="@+id/btnShowToastLong"
18 android:layout_width="fill_parent"
19 android:layout_height="wrap_content"
20 android:text="显示一个较长的Toast" />
21
22 <Button
23 android:id="@+id/btnShowToastImage"
24 android:layout_width="fill_parent"
25 android:layout_height="wrap_content"
26 android:text="显示一个图片的Toast" />
27
28 </LinearLayout>

activity_main.xml

 1 <?xml version="1.0" encoding="utf-8"?>
2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3 package="com.example.toastdemo"
4 android:versionCode="1"
5 android:versionName="1.0" >
6
7 <uses-sdk
8 android:minSdkVersion="14"
9 android:targetSdkVersion="19" />
10
11 <application
12 android:allowBackup="true"
13 android:icon="@drawable/ic_launcher"
14 android:label="@string/app_name"
15 android:theme="@style/AppTheme" >
16 <activity
17 android:name="com.example.toastdemo.MainActivity"
18 android:label="@string/app_name" >
19 <intent-filter>
20 <action android:name="android.intent.action.MAIN" />
21
22 <category android:name="android.intent.category.LAUNCHER" />
23 </intent-filter>
24 </activity>
25 </application>
26
27 </manifest>

AndroidManifest.xml

2. Notification

  学习创建Notification对象,为其指定标题、内容和图标,以及Notification的更新方法。

 1 package com.example.notificationdemo;
2
3 import android.app.Activity;
4 import android.app.Notification;
5 import android.app.NotificationManager;
6 import android.content.Context;
7 import android.os.Bundle;
8 import android.support.v4.app.NotificationCompat;
9 import android.support.v4.app.NotificationCompat.Builder;
10 import android.view.View;
11 import android.widget.Button;
12
13 public class MainActivity extends Activity {
14
15 public static final int NOTIFICATION_ID = 1234;
16 private Button btn1;
17 private int conter = 0;
18
19 @Override
20 protected void onCreate(Bundle savedInstanceState) {
21 super.onCreate(savedInstanceState);
22 setContentView(R.layout.activity_main);
23
24 btn1 = (Button) findViewById(R.id.but1);
25 btn1.setOnClickListener(new View.OnClickListener() {
26
27 @Override
28 public void onClick(View v) {
29 conter++;
30 Builder builder = new NotificationCompat.Builder(
31 MainActivity.this);
32 builder.setSmallIcon(R.drawable.ic_launcher);
33 builder.setContentTitle("你已经创建" + conter + "个新消息了!");
34 builder.setContentText("Notification~~~");
35
36 Notification notification = builder.build();
37 NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
38 manager.notify(NOTIFICATION_ID, notification);
39
40 }
41 });
42
43 }
44
45 }

MainActivity

 1 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 xmlns:tools="http://schemas.android.com/tools"
3 android:id="@+id/container"
4 android:layout_width="match_parent"
5 android:layout_height="match_parent"
6 tools:context="com.example.notificationdemo.MainActivity"
7 tools:ignore="MergeRootFrame" >
8
9 <Button
10 android:id="@+id/but1"
11 android:layout_width="fill_parent"
12 android:layout_height="wrap_content"
13 android:text="创建一个提示" />
14
15 </FrameLayout>

activity_main.xml

 1 <?xml version="1.0" encoding="utf-8"?>
2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3 package="com.example.notificationdemo"
4 android:versionCode="1"
5 android:versionName="1.0" >
6
7 <uses-sdk
8 android:minSdkVersion="14"
9 android:targetSdkVersion="19" />
10
11 <application
12 android:allowBackup="true"
13 android:icon="@drawable/ic_launcher"
14 android:label="@string/app_name"
15 android:theme="@style/AppTheme" >
16 <activity
17 android:name="com.example.notificationdemo.MainActivity"
18 android:label="@string/app_name" >
19 <intent-filter>
20 <action android:name="android.intent.action.MAIN" />
21
22 <category android:name="android.intent.category.LAUNCHER" />
23 </intent-filter>
24 </activity>
25 </application>
26
27 </manifest>

AndroidManifest.xml

09_消息通知Toast和Notification的更多相关文章

  1. 桌面消息通知:HTML5 Notification

    先上一段完整代码 //注册权限 Notification.requestPermission(function (status) { // 这将使我们能在 Chrome/Safari 中使用 Noti ...

  2. Android消息通知(notification)和PendingIntent传值

    通知栏的自定义布局:转:http://blog.csdn.net/vipzjyno1/article/details/25248021 拓展 实现自定义的通知栏效果: 这里要用到RemoteViews ...

  3. Android消息通知-Notification

    Android中常用的消息提醒,一种是Toast弹出提醒内容,一种是AlterDialog弹出框来提醒用户,还有一种就是消息通知的,用Android经常收到各种通知就是Notifation.Notif ...

  4. Android 通知机制 Toast和Notification

    Android常用的反馈系统状态信息的方式主要有三种 Toast提醒 通知栏提醒 对话框提醒 三种提醒分别适用于页面的提示.系统交互事件的通知和非常重要的提醒: 一.Toast Toast toast ...

  5. Android中的消息通知(NotificationManager和Notification)

    下面来谈谈notification,这个notification一般用在电话,短 信,邮件,闹钟铃声,在手机的状态栏上就会出现一个小图标,提示用户处理这个通知,这时手从上方滑动状态栏就可以展开并处理这 ...

  6. Android Notification 消息通知 相关资料.md

    目录 Android Notification 消息通知 相关资料 Android 5.0 Lollipop (API 21)无法正常显示通知图标,只能看到一个白色方块或灰色方块的问题 解决方案 参考 ...

  7. UWP消息通知

    在Windows 10通常是使用Toast通知方式进行的消息通知,但是在应用通知是不需要通知带有音效的,但是又不能在系统通知中心留下记录,那么需要监听ToastNotification实例的Dismi ...

  8. 使用 Windows10 自定义交互消息通知

    消息通知是最常用的应用功能之一了,但是由于平台的差异,IOS Android 以及 Windows 都有其特殊性,Android开发者在国内常常都是使用三方的一些推送服务,或者是使用自建的服务器为应用 ...

  9. 【wpf】在win10系统上弹出toast和notification

    原文:[wpf]在win10系统上弹出toast和notification 老规矩,先看效果 右下角的notification: 操作中心的notification: 整体效果: 前提条件 1.需要在 ...

随机推荐

  1. Python彩蛋、字典、列表高级用法、元类、混入、迭代器、生成器、生成式、git

    一.类与类的关系 关注公众号"轻松学编程"了解更多. is-a 继承 继承是指一个类(称为子类.子接口)继承另外一个类(称为父类.父接口)的功能, 并可以增加它自己的新功能的能力. ...

  2. Azure Data Factory(三)集成 Azure Devops 实现CI/CD

    一,引言 由于上一节文章内容过长,无法分享Azure Data Factory 的持续集成,持续发布.今天将着重介绍一下在使用 Azure DevOps Pipeline 发布,自动进行持续集成,并且 ...

  3. Hadoop安装 与 HDFS体系结构

  4. JS对象创建的几种方式整理

    ​ 本文主要介绍了JS对象创建的几种方式 第一种:Object构造函数创建 var Person = new Object(); Person.name = 'Nike'; Person.age = ...

  5. Exception in MIPS

    介绍 分支.跳转.异常(包括硬件中断)是三种改变控制流的事件. 同步异常是指程序执行到固定位置必定触发且每次现象一致的异常,如算术溢出异常.未定义指令异常.缺页异常等. 异步异常与当前执行程序无关,如 ...

  6. 【软件配置】JDK+AndroidStudio4.1开发安卓APP环境安装和配置教程详细

    目录 一.专业名词 二.搭建前资源准备 2.1 JDK资源下载 2.2 AndroidStudio下载 三.安装 3.1 JDK安装配置 3.2 AndroidStudio安装 四.创建安卓APP工程 ...

  7. CORS跨域请求:前后端分离

    1. 请求过滤器: /** * OncePerRequestFilter保证在任何Servlet容器中都是一个请求只执行一次的过滤器. */ public class CorsFilter exten ...

  8. linux nf_conntrack 连接跟踪机制 2

    连接跟踪初始化 基础参数的初始化:nf_conntrack_standalone_init 会调用nf_conntrack_init_start 完成连接跟踪基础参数的初始化, hash slab 扩 ...

  9. 224、Basic Calculator

    Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...

  10. socket套接字(字节序、地址转换)

    什么是socket: socket可以看成是用户进程与内核网络协议栈的编程接口. socket不仅可以用于本机的进程间通信,还可以用于网络上 不同主机之间的进程通信.IPv4套接口地址结构 struc ...