我的android 第三天 - 自定义Toast

本文介绍了如何在Android应用中实现自定义Toast消息和系统通知。通过创建按钮并为其添加监听器,可以触发不同类型的提示信息展示。具体包括构建自定义视图、设置Toast样式以及发送带有操作的通知。

我的android 第三天 -  自定义Toast

今天学自定义Toast。好吧,原谅我周末偷懒了!先弄2个Button。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/toast_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="17dp"
        android:text="吐司"
        android:textSize="30sp" />

    <Button
        android:id="@+id/notice_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/toast_btn"
        android:layout_below="@+id/toast_btn"
        android:layout_marginTop="46dp"
        android:text="通知" />

</RelativeLayout>
在activity里面找到这2个Button ,添加监听

        toastBtn=(Button) findViewById(R.id.toast_btn);
        noticeBtn=(Button) findViewById(R.id.notice_btn);
        //给按钮添加监听
        toastBtn.setOnClickListener(this);
        noticeBtn.setOnClickListener(this);

判断单击了哪个View
public void onClick(View v) {
		//判断单击了哪个View
		switch (v.getId()) {
		case R.id.toast_btn:
			//显示Toast
			toast(v);	
			break;
		case R.id.notice_btn:
			notice(v);
		}	
	}

private void notice(View v) {
		NotificationManager mgr=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		Notification notification=new Notification(R.drawable.sym_call_missed, 
				"你有一个未接电话", System.currentTimeMillis());
		//意图
		Intent intent=new Intent(context,DetailActivity.class);
		//条件触发意图,转移activity
		PendingIntent pi=PendingIntent.getActivity(context, 0, intent, 0);
		notification.setLatestEventInfo(context, "未接电话", "查看来源", pi);
		//默认提示的声音
		notification.defaults=Notification.DEFAULT_SOUND;
		//发出通知
		mgr.notify(1, notification);

		
	}

private void toast(View v) {
		Toast toast=new Toast(context);
		//设置view(就是Toast显示的界面)
		//构建一个线性布局
		LinearLayout layout=new LinearLayout(context);
		layout.setBackgroundResource(R.drawable.bg_yellow);
		layout.setGravity(Gravity.CENTER);
		//设置此布局为水平线性布局
		layout.setOrientation(LinearLayout.HORIZONTAL);
		//构建一个图片
		ImageView image=new ImageView(context);
		image.setBackgroundResource(R.drawable.ic_toast);
		//添加图片到布局
		layout.addView(image);
		//构建一个文本资源
		TextView text=new TextView(context);
		text.setText("冰激凌");
		layout.addView(text);
		toast.setView(layout);
		//设置子控件的位置
		toast.setGravity(Gravity.CENTER, 0, 0);//偏移量
		//设置Toast的显示时间
		toast.setDuration(Toast.LENGTH_LONG);
		//显示吐司
		toast.show();	
	}


效果演示:

点击第一个button


点击第二个button



点击这条信息跳转到DetailActivity



大概就这样吧。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值