【转】通知 Toast详细用法(显示view)

原文网址:http://www.pocketdigi.com/20100904/87.html

今天学习Android通知 Toast的用法,Toast在手机屏幕上向用户显示一条信息,一段时间后信息会自动消失。信息可以是简单的文本,也可以是复杂的图片及其他内容(显示一个view)。
看效果图:

今天演示的有两种用法,如上图
main.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <Button android:id="@+id/button1"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="Toast显示View"
  11. />
  12. <Button android:id="@+id/button2"
  13. android:layout_width="fill_parent"
  14. android:layout_height="wrap_content"
  15. android:text="Toast直接输出"
  16. />
  17. </LinearLayout>

两个按钮,很简单
程序代码:

  1. package com.pocketdgig.toast;
  2.  
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.os.Bundle;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.View.OnClickListener;
  9. import android.widget.Button;
  10. import android.widget.TextView;
  11. import android.widget.Toast;
  12.  
  13. public class main extends Activity {
  14. /** Called when the activity is first created. */
  15. @Override
  16. public void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.main);
  19. Button button1=(Button)findViewById(R.id.button1);
  20. button1.setOnClickListener(bt1lis);
  21. Button button2=(Button)findViewById(R.id.button2);
  22. button2.setOnClickListener(bt2lis);
  23. }
  24. OnClickListener bt1lis=new OnClickListener(){
  25.  
  26. @Override
  27. public void onClick(View v) {
  28. showToast();
  29. }
  30. };
  31. OnClickListener bt2lis=new OnClickListener(){
  32. @Override
  33. public void onClick(View v) {
  34. Toast.makeText(main.this,"直接输出测试", Toast.LENGTH_LONG).show();
  35. }
  36. };
  37. public void showToast(){
  38. LayoutInflater li=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  39. View view=li.inflate(R.layout.toast,null);
  40. //把布局文件toast.xml转换成一个view
  41. Toast toast=new Toast(this);
  42. toast.setView(view);
  43. //载入view,即显示toast.xml的内容
  44. TextView tv=(TextView)view.findViewById(R.id.tv1);
  45. tv.setText("Toast显示View内容");
  46. //修改TextView里的内容
  47. toast.setDuration(Toast.LENGTH_SHORT);
  48. //设置显示时间,长时间Toast.LENGTH_LONG,短时间为Toast.LENGTH_SHORT,不可以自己编辑
  49. toast.show();
  50. }
  51. }

下面是toast.xml的内容:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <ImageView android:src="@drawable/toast"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. />
  11. <TextView android:id="@+id/tv1"
  12. android:text=""
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. />
  16. </LinearLayout>

源代码下载:[download id=”4″]

© 2010, 冰冻鱼. 请尊重作者劳动成果,复制转载保留本站链接! 应用开发笔记

原文地址:https://www.cnblogs.com/wi100sh/p/4464382.html