android 信息提示框 Toast

本文介绍了一种简短信息提示框组件——Toast,并通过一个示例程序详细展示了如何在Android应用中创建并使用Toast来显示长、短时间的消息提示。示例包括了在XML布局文件中定义按钮和在Java代码中实现点击事件来触发Toast显示的方法。



 

Toast是一个简短的信息提示框,可以在一定的时间内进行显示,而后自动进行隐藏的一种组件。

 

 

在main.xml中

<?xml version="1.0" encoding="utf-8"?>

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

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

 

    <Button

        android:id="@+id/butA"

        android:layout_marginTop="10dp"

        android:layout_marginLeft="8dp"

        android:layout_marginRight="8dp"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="长时间显示Toast" />

    <Button

        android:id="@+id/butB"

        android:layout_marginTop="8dp"

        android:layout_marginLeft="8dp"

        android:layout_marginRight="8dp"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="短时间显示Toast" />

 

</LinearLayout>

 

 

 

在MyToastDemo.java程序中

package com.tarena.toast;

 

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.Toast;

public class MyToastDemo extends Activity {

  private Button butA = null;

  private Button butB = null;

 

  @Override

  public void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);

     super.setContentView(R.layout.main);

     this.butA = (Button) super.findViewById(R.id.butA);

     this.butB = (Button) super.findViewById(R.id.butB);

     this.butA.setOnClickListener(new OnClickListenerImplLong()) ;

     this.butB.setOnClickListener(new OnClickListenerImplShort()) ;

  }

 

  private class OnClickListenerImplLong implements OnClickListener {

 

     public void onClick(View v) {

       Toast.makeText(MyToastDemo.this, "长时间显示的Toast信息提示框",

            Toast.LENGTH_LONG).show();

     }

 

  }

 

  private class OnClickListenerImplShort implements OnClickListener {

 

     public void onClick(View v) {

       Toast.makeText(MyToastDemo.this, "短时间显示的Toast信息提示框",

            Toast.LENGTH_SHORT).show();

     }

 

  }

}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值