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. Python分析三个月微博热搜数据带你回顾20
  2. Android:浅谈 mvp-clean 架构
  3. 我安装Python第三方库的几种姿势
  4. COVID-19每日数据|04-05
  5. Python使用pdb更优雅的调试代码
  6. COVID-19每日据整理|03-31
  7. 写在Pandas120题系列完结|附PDF版本源码下
  8. 一个例子带你入门Python装饰器
  9. 动画:面试官问我如何在 1 亿数据中快速查
  10. 5分钟学会Pandas中iloc/loc/ix区别