1. 首先新建一个Android程序

  2. activity_main 文件的代码如下

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:orientation="vertical"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context="com.example.toast.MainActivity" >


    <Button

    android:id="@+id/totastshort"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="按钮1" />


    <Button

    android:id="@+id/totastlong"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="long" />


    </LinearLayout>

  3. MainActivity.java文件的代码如下


    package com.example.toast;


    import android.app.Activity;

    import android.os.Bundle;

import android.view.Gravity;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.widget.Button;

import android.widget.ImageView;

import android.widget.Toast;


public class MainActivity extends Activity {


private Button totast1;

private Button totast2;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

totast1=(Button) findViewById(R.id.totastshort);

totast1.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

Toast t=Toast.makeText(MainActivity.this,"hello my first short totast",Toast.LENGTH_SHORT);

t.setGravity(Gravity.CENTER, -10, 0);

t.show();

}

});

totast2=(Button) findViewById(R.id.totastlong);

totast2.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

Toast t1=Toast.makeText(MainActivity.this, "long totast",Toast.LENGTH_LONG);

ImageView i=new ImageView(MainActivity.this);

i.setImageResource(R.drawable.ic_launcher);

t1.setView(i);

t1.show();

}

} );

}


}

4.注意:最后不要忘记totast的show().


更多相关文章

  1. android apk 升级代码
  2. Android震动和播放资源文件中的声音文件
  3. Android文件管理器开发对各类文件的打开以及处理
  4. xml文件
  5. android sdcard存储方案(基于fuse文件系统):之一
  6. android读取data/data/包名/file路径下的txt文件
  7. Android 相机2之常用工具代码(预览方向、预览尺寸、全屏显示、分
  8. [置顶] Android防火墙+流量统计代码
  9. Android应用程序获取ROOT权限代码

随机推荐

  1. Javascript函数的4种调用方法详解
  2. React.js中的setState vs replaceState
  3. 选择无线电输入时,无法提交带有JavaScript
  4. 【JavaScript】中两个小括号 ()() 是什么
  5. jQuery的几大难点
  6. 利用javascript实现遍历xml文件的代码实
  7. 一个html+css+js的轮播图片 -- 仅供参考
  8. 输入自动完成或Rails中的新功能
  9. Jquery中动态添加元素的绑定事件不起作用
  10. jqGrid 数据加载完事件 loadComplete 使