Android Toast Example

本文介绍了Android中Toast组件的基本使用方法,包括如何创建短时间和长时间显示的Toast消息,并提供了普通Toast视图的实例代码。此外,还简要提及了自定义Toast视图的方法。

在 Android 中 Toast 是一个弹出的信息通知(如短信发送成功的提示,如记事本的“你刚才的内容已经保存”),功能 display a certain amount of time, and automtaically fades in and out, 
most people just use it for debugging purpose.画外音:绝大多数人是如何用 Toast 来 debugging 的呢???


创建 Toast 信息的核心代码片段:
//短时间内“稍纵即逝”

Toast.makeText(getApplicationContext(), "your msg", Toast.LENGTH_SHORT).show();

//长时间内“稍纵即逝”
Toast.makeText(getApplicationContext(), "your msg", Toast.LENGTH_LONG).show();




本教程将展现,两个 Toast 例子:
1、普通的(Normal Toast view.)
2、自定义的(Custom Toast view.)





1. 普通的(Normal Toast View)

File : res/layout/activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"

    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="show Toast" />

</LinearLayout>



File: MainActivity.java

package com.jiangge.toastdemo;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
	private Button button;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button = (Button) super.findViewById(R.id.button);
		button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Context context = getApplicationContext();
				CharSequence text = "Button is clicked, toast coming!";
				int duration = Toast.LENGTH_SHORT;

				Toast toast = Toast.makeText(context, text, duration);
				toast.show();
				
			}
		});
		
	}

}


运行结果:



2 TODO 自定义





===================================

看下官网文档:

Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;

Toast toast = Toast.makeText(context, text, duration);
toast.show();
也可以使用方法串的形式:

Toast.makeText(context, text, duration).show();



Note:其实省去中间变量,代码可以简化很多:

package com.jiangge.toastdemo;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
	private Button button;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button = (Button) super.findViewById(R.id.button);
		button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
//				Context context = getApplicationContext();
//				CharSequence text = "Button is clicked, toast coming!";
//				int duration = Toast.LENGTH_SHORT;

				Toast.makeText(getApplicationContext(), "Button is clicked, toast coming!", Toast.LENGTH_SHORT).show();
			}
		});
		
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值