简易消息提示框Toast和通知的使用

随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)

Android中的Toast是一种简易的消息提示框。和Dialog不一样的是,Toast是没有焦点的,toast提示框不能被用户点击,而且Toast显示的时间有限,toast会根据用户设置的显示时间后自动消失。

下面通过一个实例来演示Toast以及通知的使用:

package com.lovo;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class DialogActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// 获得发送消息的按钮组件
		Button btnToast = (Button) findViewById(R.id.btn_toast);
		btnToast.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// 打开消息提示,LENGTH_LONG消息停留的时间长,LENGTH_SHORT消息停留的时间短
				Toast.makeText(DialogActivity.this, "hello", Toast.LENGTH_LONG)
						.show();
			}
		});

		// 获得发送通知的按钮组件
		Button btnNotify = (Button) findViewById(R.id.btn_notify);
		btnNotify.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 1.创建通知管理对象
				NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
				// 2.创建通知对象
				Notification no = new Notification();
				// 设置通知的提示信息
				no.tickerText = "你有新的消息";
				// 设置通知的图标
				no.icon = R.drawable.ic_launcher;
				// 设置默认的提示方式,DEFAULT_ALL表示声音提示、震动提示、闪光提示
				// no.defaults=Notification.DEFAULT_ALL;
				// 设置运行状态标识
				// FLAG_NO_CLEAR:该通知无法被清除
				// no.flags=Notification.FLAG_NO_CLEAR;
				// FLAG_ONGOING_EVENT:显示在正在运行的通知栏里
				// no.flags=Notification.FLAG_ONGOING_EVENT;
				// FLAG_AUTO_CANCEL:通知可以被清除掉
				no.flags = Notification.FLAG_AUTO_CANCEL;
				// 3.获取Intent对象
				PendingIntent conInt = PendingIntent.getActivity(
						getApplicationContext(), 0, getIntent(),
						PendingIntent.FLAG_CANCEL_CURRENT);
				// 4.设置显示在通知栏里的信息
				no.setLatestEventInfo(getApplicationContext(), "天气预报", "多云转晴",
						conInt);
				// 5.执行通知,第一参数为该通知的唯一标识
				nm.notify(12, no);
			}
		});
	}
}


布局XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn_notify"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送通知" />

    <Button
        android:id="@+id/btn_toast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Toast" />

</LinearLayout>


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

u010142437

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值