09_消息通知Toast和Notification
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的更多相关文章
- 桌面消息通知:HTML5 Notification
先上一段完整代码 //注册权限 Notification.requestPermission(function (status) { // 这将使我们能在 Chrome/Safari 中使用 Noti ...
- Android消息通知(notification)和PendingIntent传值
通知栏的自定义布局:转:http://blog.csdn.net/vipzjyno1/article/details/25248021 拓展 实现自定义的通知栏效果: 这里要用到RemoteViews ...
- Android消息通知-Notification
Android中常用的消息提醒,一种是Toast弹出提醒内容,一种是AlterDialog弹出框来提醒用户,还有一种就是消息通知的,用Android经常收到各种通知就是Notifation.Notif ...
- Android 通知机制 Toast和Notification
Android常用的反馈系统状态信息的方式主要有三种 Toast提醒 通知栏提醒 对话框提醒 三种提醒分别适用于页面的提示.系统交互事件的通知和非常重要的提醒: 一.Toast Toast toast ...
- Android中的消息通知(NotificationManager和Notification)
下面来谈谈notification,这个notification一般用在电话,短 信,邮件,闹钟铃声,在手机的状态栏上就会出现一个小图标,提示用户处理这个通知,这时手从上方滑动状态栏就可以展开并处理这 ...
- Android Notification 消息通知 相关资料.md
目录 Android Notification 消息通知 相关资料 Android 5.0 Lollipop (API 21)无法正常显示通知图标,只能看到一个白色方块或灰色方块的问题 解决方案 参考 ...
- UWP消息通知
在Windows 10通常是使用Toast通知方式进行的消息通知,但是在应用通知是不需要通知带有音效的,但是又不能在系统通知中心留下记录,那么需要监听ToastNotification实例的Dismi ...
- 使用 Windows10 自定义交互消息通知
消息通知是最常用的应用功能之一了,但是由于平台的差异,IOS Android 以及 Windows 都有其特殊性,Android开发者在国内常常都是使用三方的一些推送服务,或者是使用自建的服务器为应用 ...
- 【wpf】在win10系统上弹出toast和notification
原文:[wpf]在win10系统上弹出toast和notification 老规矩,先看效果 右下角的notification: 操作中心的notification: 整体效果: 前提条件 1.需要在 ...
随机推荐
- python实现类的多态
多态 关注公众号"轻松学编程"了解更多. 1.多态使用 一种事物的多种体现形式,举例:动物有很多种 注意: 继承是多态的前提 函数重写就是多态的体现形式 演示:重写Animal类 ...
- 水题挑战1:NOIP 2013 选择客栈
丽江河边有\(n\) 家很有特色的客栈,客栈按照其位置顺序从 \(1\) 到 \(n\) 编号.每家客栈都按照某一种色调进行装饰(总共 \(k\) 种,用整数 \(0 \sim k-1\) 表示),且 ...
- [Luogu P4124] [CQOI2016]手机号码 (数位DP)
题面 传送门:洛咕 Solution 感谢神仙@lizbaka的教学 这题是数位DP的非常非常模板的题目,只是状态有点多 . 这题我使用记忆化搜索实现的 中国有句古话说的好,有多少个要求就设多少个状态 ...
- mysql处理查询请求的步骤
服务端处理客户端的查询请求大致需要三个步骤: 连接管理 客户端连接服务端时,服务端会为其分配一个线程,客户端断开连接不会回收线程(避免频繁创建销毁的性能问题),服务端一直等待客户端发来消息(文本消息) ...
- 凯撒密码(Java)
事实上就是把每个字母偏移一下而已,并且字符不限于a-zA-z,可以是别的,那就很显而易见了,代码如下:定义一个Caesar密码类,成员变量只有密钥,也就是偏移量key 代码如下: public cla ...
- 【SpringCloud】06.Eureka 总结
1.两个注解: @EnableEurekaServer--在启动类上添加 @EnableDiscoveryClient或@EnableEurekaClient--启动类加 因为Eureka支持多种注册 ...
- 【SpringBoot】11-1.Springboot整合Springmvc+Mybatis增删改查操作(下)
整合过程:https://www.isdxh.com/68.html 一.增--增加用户 1.创建实体类 package com.dxh.pojo; public class Users { priv ...
- 腾讯云--腾讯云sdk-实现脚本修改腾讯云负载均衡权重
一.请确认你的当前python环境为python 2.x 获取 python 版本的方法 (linux shell) # python -v python 2.7.11 二.CLB SDK下载与配置 ...
- 咀嚼Lock和Synchronized锁
1.Synchronized锁 底层是monitor监视器,每一个对象再创建的时候都会常见一个monitor监视器,在使用synchronized代码块的时候,会在代码块的前后产生一个monitorE ...
- 烧录时发生:permission denied:'/dev/ttyUSB0'问题的解决
在执行make flash的过程中出现错误: 解决办法: sudo chmod -R 777 /dev/ttyUSB0 这种设置在下次使用的,又会出现这种问题,还要重新设置 永久性的设置可以使用下面这 ...