今天学习Android通知 Toast的用法,Toast在手机屏幕上向用户显示一条信息,一段时间后信息会自动消失。信息可以是简单的文本,也可以是复杂的图片及其他内容(显示一个view)。
看效果图:
今天演示的有两种用法,如上图
main.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Toast显示View" /> <Button android:id="@+id/button2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Toast直接输出" /> </LinearLayout> |
两个按钮,很简单
程序代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
package com.pocketdgig.toast; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class main extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button button1=(Button)findViewById(R.id.button1); button1.setOnClickListener(bt1lis); Button button2=(Button)findViewById(R.id.button2); button2.setOnClickListener(bt2lis); } OnClickListener bt1lis=new OnClickListener(){ @Override public void onClick(View v) { showToast(); } }; OnClickListener bt2lis=new OnClickListener(){ @Override public void onClick(View v) { Toast.makeText(main.this,"直接输出测试", Toast.LENGTH_LONG).show(); } }; public void showToast(){ LayoutInflater li=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view=li.inflate(R.layout.toast,null); //把布局文件toast.xml转换成一个view Toast toast=new Toast(this); toast.setView(view); //载入view,即显示toast.xml的内容 TextView tv=(TextView)view.findViewById(R.id.tv1); tv.setText("Toast显示View内容"); //修改TextView里的内容 toast.setDuration(Toast.LENGTH_SHORT); //设置显示时间,长时间Toast.LENGTH_LONG,短时间为Toast.LENGTH_SHORT,不可以自己编辑 toast.show(); } } |
下面是toast.xml的内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:src="@drawable/toast" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv1" android:text="" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
|
相关推荐
实际上用户本意只是想关闭Notification,但是Toast的show方法中有调用INotificationManager这个类,而这个类在用户关闭消息通知权限的同时被禁用了,所以我们的吐司无法显示。 Toast.show() 效果图 自定义Toast...
13.2 使用multidex来解决方法数越界 455 13.3 Android的动态加载技术 463 13.4 反编译初步 469 13.4.1 使用dex2jar和jd-gui反编译apk 470 13.4.2 使用apktool对apk进行二次打包 470 第14章 JNI和NDK编程 473 ...
13.2 使用multidex来解决方法数越界 / 455 13.3 Android的动态加载技术 / 463 13.4 反编译初步 / 469 13.4.1 使用dex2jar和jd—gui反编译apk / 470 13.4.2 使用apktool对apk进行二次打包 / 470 第14章 JNI...
/ 200 4.4.2 自定义View须知 / 201 4.4.3 自定义View示例 / 202 4.4.4 自定义View的思想 / 217 第5章 理解Remote Views / 218 5.1 Remote Views的应用 / 218 5.1.1 Remote Views在通知栏上的应用 / 219 5.1.2...
24.4.5 使用语言方法 24.5 参考资料 24.6 小结 第25章 触摸屏 25.1 MotionEvent 25.1.1 MotionEvent 对象 25.1.2 回收MotionEvent 25.1.3 使用VelocityTracker 25.1.4 探索拖放操作 25.2...
24.4.5 使用语言方法 24.5 参考资料 24.6 小结 第25章 触摸屏 25.1 MotionEvent 25.1.1 MotionEvent 对象 25.1.2 回收MotionEvent 25.1.3 使用VelocityTracker 25.1.4 探索拖放操作 25.2...
3.6.1 秘诀23:使用toast 在屏幕上显示简短消息 61 3.6.2 秘诀24:使用alert 对话框 61 3.6.3 秘诀25:在状态栏中显示通知 62 第4 章 用户界面布局 65 4.1 资源目录及其基本属性 65 4.2 view 和viewgroup 67 ...
14.2.4 创建View并显示 14.3 曲线图 14.4 柱状图 14.5 饼图 14.5.1 一层的饼图 14.5.2 两层的饼图 14.6 XY组合图 14.7 本章小结 第15章 专题应用 15.1 地图 15.1.1 定位API 15.1.2 地图API 15.2 蓝牙API 15.2.1 蓝牙...
5.7 图文可视化提醒——Toast与LinearLayoutView 5.8 状态栏的图标与文字提醒——NotificationManager与Notification对象的应用 5.9 搜索手机通讯录自动完成——使用ContentResolver 5.10 取得联系人资料——...
5.7 图文可视化提醒——Toast与LinearLayoutView 5.8 状态栏的图标与文字提醒——NotificationManager与Notification对象的应用 5.9 搜索手机通讯录自动完成——使用ContentResolver 5.10 取得联系人资料——...
5.7 图文可视化提醒——Toast与LinearLayoutView 5.8 状态栏的图标与文字提醒——NotificationManager与Notification对象的应用 5.9 搜索手机通讯录自动完成——使用ContentResolver 5.10 取得联系人资料——...
5.7 图文可视化提醒——Toast与LinearLayoutView 5.8 状态栏的图标与文字提醒——NotificationManager与Notification对象的应用-p178 5.9 搜索手机通讯录自动完成——使用ContentResolver-p82 5.10 取得联系人资料...
5.7 图文可视化提醒——Toast与LinearLayoutView 5.8 状态栏的图标与文字提醒——NotificationManager与Notification对象的应用 5.9 搜索手机通讯录自动完成——使用ContentResolver 5.10 取得联系人资料——...
5.7 图文可视化提醒——Toast与LinearLayoutView 5.8 状态栏的图标与文字提醒——NotificationManager与Notification对象的应用 5.9 搜索手机通讯录自动完成——使用ContentResolver 5.10 取得联系人资料——...