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

时间:2023-03-09 08:07:38
【转】通知 Toast详细用法(显示view)

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

今天学习Android通知 Toast的用法,Toast在手机屏幕上向用户显示一条信息,一段时间后信息会自动消失。信息可以是简单的文本,也可以是复杂的图片及其他内容(显示一个view)。
看效果图:
【转】通知 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. import android.app.Activity;
  3. import android.content.Context;
  4. import android.os.Bundle;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.widget.Button;
  9. import android.widget.TextView;
  10. import android.widget.Toast;
  11. public class main extends Activity {
  12. /** Called when the activity is first created. */
  13. @Override
  14. public void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.main);
  17. Button button1=(Button)findViewById(R.id.button1);
  18. button1.setOnClickListener(bt1lis);
  19. Button button2=(Button)findViewById(R.id.button2);
  20. button2.setOnClickListener(bt2lis);
  21. }
  22. OnClickListener bt1lis=new OnClickListener(){
  23. @Override
  24. public void onClick(View v) {
  25. showToast();
  26. }
  27. };
  28. OnClickListener bt2lis=new OnClickListener(){
  29. @Override
  30. public void onClick(View v) {
  31. Toast.makeText(main.this,"直接输出测试", Toast.LENGTH_LONG).show();
  32. }
  33. };
  34. public void showToast(){
  35. LayoutInflater li=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  36. View view=li.inflate(R.layout.toast,null);
  37. //把布局文件toast.xml转换成一个view
  38. Toast toast=new Toast(this);
  39. toast.setView(view);
  40. //载入view,即显示toast.xml的内容
  41. TextView tv=(TextView)view.findViewById(R.id.tv1);
  42. tv.setText("Toast显示View内容");
  43. //修改TextView里的内容
  44. toast.setDuration(Toast.LENGTH_SHORT);
  45. //设置显示时间,长时间Toast.LENGTH_LONG,短时间为Toast.LENGTH_SHORT,不可以自己编辑
  46. toast.show();
  47. }
  48. }

下面是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, 冰冻鱼. 请尊重作者劳动成果,复制转载保留本站链接! 应用开发笔记