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

[plain] view plain copy
  1. <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <Button
  6. android:id="@+id/toast_btn"
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:layout_alignParentTop="true"
  10. android:layout_centerHorizontal="true"
  11. android:layout_marginTop="17dp"
  12. android:text="吐司"
  13. android:textSize="30sp"/>
  14. <Button
  15. android:id="@+id/notice_btn"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:layout_alignLeft="@+id/toast_btn"
  19. android:layout_below="@+id/toast_btn"
  20. android:layout_marginTop="46dp"
  21. android:text="通知"/>
  22. </RelativeLayout>

在activity里面找到这2个Button ,添加监听

[java] view plain copy
  1. toastBtn=(Button)findViewById(R.id.toast_btn);
  2. noticeBtn=(Button)findViewById(R.id.notice_btn);
  3. //给按钮添加监听
  4. toastBtn.setOnClickListener(this);
  5. noticeBtn.setOnClickListener(this);


判断单击了哪个View

[java] view plain copy
  1. publicvoidonClick(Viewv){
  2. //判断单击了哪个View
  3. switch(v.getId()){
  4. caseR.id.toast_btn:
  5. //显示Toast
  6. toast(v);
  7. break;
  8. caseR.id.notice_btn:
  9. notice(v);
  10. }
  11. }

[java] view plain copy
  1. privatevoidnotice(Viewv){
  2. NotificationManagermgr=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
  3. Notificationnotification=newNotification(R.drawable.sym_call_missed,
  4. "你有一个未接电话",System.currentTimeMillis());
  5. //意图
  6. Intentintent=newIntent(context,DetailActivity.class);
  7. //条件触发意图,转移activity
  8. PendingIntentpi=PendingIntent.getActivity(context,0,intent,0);
  9. notification.setLatestEventInfo(context,"未接电话","查看来源",pi);
  10. //默认提示的声音
  11. notification.defaults=Notification.DEFAULT_SOUND;
  12. //发出通知
  13. mgr.notify(1,notification);
  14. }

[java] view plain copy
  1. privatevoidtoast(Viewv){
  2. Toasttoast=newToast(context);
  3. //设置view(就是Toast显示的界面)
  4. //构建一个线性布局
  5. LinearLayoutlayout=newLinearLayout(context);
  6. layout.setBackgroundResource(R.drawable.bg_yellow);
  7. layout.setGravity(Gravity.CENTER);
  8. //设置此布局为水平线性布局
  9. layout.setOrientation(LinearLayout.HORIZONTAL);
  10. //构建一个图片
  11. ImageViewimage=newImageView(context);
  12. image.setBackgroundResource(R.drawable.ic_toast);
  13. //添加图片到布局
  14. layout.addView(image);
  15. //构建一个文本资源
  16. TextViewtext=newTextView(context);
  17. text.setText("冰激凌");
  18. layout.addView(text);
  19. toast.setView(layout);
  20. //设置子控件的位置
  21. toast.setGravity(Gravity.CENTER,0,0);//偏移量
  22. //设置Toast的显示时间
  23. toast.setDuration(Toast.LENGTH_LONG);
  24. //显示吐司
  25. toast.show();
  26. }



好吧,就这样吧。。

更多相关文章

  1. Android(安卓)强制设置应用横屏或竖屏
  2. Android的设置界面及Preference使用
  3. TextView
  4. Android(安卓)剪切板操作
  5. Android(安卓)ImageView、TextureView、自定义View显示图片
  6. Android(安卓)SystemUI 信号栏后添加信号图标
  7. 控制软键盘显示和隐藏
  8. Android控件开发之四----ListView(2)
  9. Android(安卓)TextView 动态设置DrawableLeft

随机推荐

  1. 关于android 图像格式问题
  2. 错误信息:Error generating final archive
  3. android中实现tab标签切换时的一些效果
  4. EditText的属性说明
  5. Android(安卓)存储(本地存储 SD卡存储 Sha
  6. 安卓学习笔记(15)-android文件分享在andr
  7. Android——线程和线程池
  8. android 自定义标签的使用,实现扁平化UI设
  9. android 多渠道打包
  10. FFMPEG for android 文章汇总