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. SpringBoot第五集:整合监听器/过滤器和拦截器(2020最新最易懂)

    SpringBoot第五集:整合监听器/过滤器和拦截器(2020最新最易懂) 在实际开发过程中,经常会碰见一些比如系统启动初始化信息.统计在线人数.在线用户数.过滤敏/高词汇.访问权限控制(URL级别 ...

  2. 转载-Eclipse导入第三方库的方法

    作者:wyf_phper 原文:https://blog.csdn.net/qq_32985981/article/details/49976193 一:导入*.jar包步骤:将下载好的jar包复制到 ...

  3. 自己常用的Content-Type几种值用法

    Content-Type 的值类型: application/json:消息主体是序列化后的 JSON 字符串 这里要注意的是 我在使用webapi,前台使用$.ajax的时候 假如我要传递的数据为 ...

  4. vue实现带logo的二维码/商品条形码/打印商品吊牌

    一.带logo的二维码 1.安装 npm install vue-qr --save 2.在页面或组件中使用 <template> <div id="qrcode" ...

  5. cmd的基本命令

    使用方法Windows键+R键输入cmd回车 Windows 系统常用小工具 工具名称 cmd 命令 含义 计算器 calc 与 "工具名称" 相同 记事本 notepad 与 & ...

  6. 跟我一起学Redis之Redis事务简单了解一下

    前言 关系数据库中的事务,小伙伴们应该是不陌生了,不管是在开发还是在面试过程中,总有两个问题逃不掉: 说说事务的特性: 事务隔离级别是怎么一回事? 事务处理不好,数据就可能不准确,最终就会导致业务出问 ...

  7. DB2添加联合主键

    CREATE TABLE EQUIPMENT_DAILY_CAL( EQU_DATE TIME NOT NULL ,/*日期*/ SEQ_ID INTEGER NOT NULL,/*序号 */ FAU ...

  8. laravel 控制器中获取不到session

    protected $middleware = [ \Illuminate\Session\Middleware\StartSession::class, ]; 在 kernel.php中 加入Sta ...

  9. Spider--补充--Re模块_1

    # @ Author : Collin_PXY # 正则表达式: import re # 1,分步写法: # 1)rule.search(string) pattern='各小区' rule=re.c ...

  10. MQ-gogogo

    1. RocketMQ https://github.com/alibaba/RocketMQ/wiki/quick-start 2. RabbitMQ https://www.rabbitmq.co ...