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

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

<?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>

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

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(){ @Overridepublic void onClick(View v) {showToast();}     };    OnClickListener bt2lis=new OnClickListener(){@Overridepublic 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的内容:

<?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>

更多相关文章

  1. ionic项目中手机状态栏显示使用$cordovaStatusbar插件
  2. 【Android工具】被忽略的UI检视利器:Hierarchy Viewer
  3. Android中persistent属性用法详解
  4. Android(安卓)Framework Boot Up Overview(Android系统框架启动流
  5. android讲义2之简单图片浏览器且有截图效果(对ImageView使用)
  6. Android(安卓)ContentProvider详解
  7. PreferenceActivity 全接触
  8. Android之PullToRefresh(ListView 、GridView 、WebView)使用详
  9. 一个Activity的显示过程总结(二)

随机推荐

  1. [Android]从新旧API看android代码演进
  2. Android获取外置SD卡读写路径
  3. Android Studio 中关于NDK编译及jni head
  4. Android之AsyncTask的内存泄露问题
  5. Android系统源码目录解析
  6. Android软键盘处理开发规范
  7. 第一个Android程序
  8. 开机提示“Android正在升级...”
  9. Android伸手党系列之八:Android常用开发问
  10. android 的ViewPager的预加载机制及解决