原链接:http://www.cnblogs.com/GnagWang/archive/2010/11/26/1888762.html
Toast是Android中用来显示显示信息的一种机制,和Dialog不一样的是,Toast是没有焦点的,而且Toast显示的时间有限,过一定的时间就会自动消失。下面用一个实例来看看如何使用Toast。
1.默认效果

代码
Toast.makeText(getApplicationContext(), "默认Toast样式",
Toast.LENGTH_SHORT).show();
2.自定义显示位置效果

代码
toast = Toast.makeText(getApplicationContext(),
"自定义位置Toast", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
3.带图片效果

代码
toast = Toast.makeText(getApplicationContext(),
"带图片的Toast", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
LinearLayout toastView = (LinearLayout) toast.getView();
ImageView imageCodeProject = new ImageView(getApplicationContext());
imageCodeProject.setImageResource(R.drawable.icon);
toastView.addView(imageCodeProject, 0);
toast.show();
4.完全自定义效果

代码
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom,
(ViewGroup) findViewById(R.id.llToast));
ImageView image = (ImageView) layout
.findViewById(R.id.tvImageToast);
image.setImageResource(R.drawable.icon);
TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);
title.setText("Attention");
TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
text.setText("完全自定义Toast");
toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
5.其他线程

代码
new Thread(new Runnable() {
public void run() {
showToast();
}
}).start();
完整代码
1.Main,java
package com.wjq.toast;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class Main extends Activity implements OnClickListener {
Handler handler = new Handler();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.btnSimpleToast).setOnClickListener(this);
findViewById(R.id.btnSimpleToastWithCustomPosition).setOnClickListener(
this);
findViewById(R.id.btnSimpleToastWithImage).setOnClickListener(this);
findViewById(R.id.btnCustomToast).setOnClickListener(this);
findViewById(R.id.btnRunToastFromOtherThread).setOnClickListener(this);
}
public void showToast() {
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "我来自其他线程!",
Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onClick(View v) {
Toast toast = null;
switch (v.getId()) {
case R.id.btnSimpleToast:
Toast.makeText(getApplicationContext(), "默认Toast样式",
Toast.LENGTH_SHORT).show();
break;
case R.id.btnSimpleToastWithCustomPosition:
toast = Toast.makeText(getApplicationContext(),
"自定义位置Toast", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
break;
case R.id.btnSimpleToastWithImage:
toast = Toast.makeText(getApplicationContext(),
"带图片的Toast", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
LinearLayout toastView = (LinearLayout) toast.getView();
ImageView imageCodeProject = new ImageView(getApplicationContext());
imageCodeProject.setImageResource(R.drawable.icon);
toastView.addView(imageCodeProject, 0);
toast.show();
break;
case R.id.btnCustomToast:
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom,
(ViewGroup) findViewById(R.id.llToast));
ImageView image = (ImageView) layout
.findViewById(R.id.tvImageToast);
image.setImageResource(R.drawable.icon);
TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);
title.setText("Attention");
TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
text.setText("完全自定义Toast");
toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
break;
case R.id.btnRunToastFromOtherThread:
new Thread(new Runnable() {
public void run() {
showToast();
}
}).start();
break;
}
}
}
2.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" android:padding="5dip" android:gravity="center">
<Button android:layout_height="wrap_content"
android:layout_width="fill_parent" android:id="@+id/btnSimpleToast"
android:text="默认"></Button>
<Button android:layout_height="wrap_content"
android:layout_width="fill_parent" android:text="自定义显示位置"
android:id="@+id/btnSimpleToastWithCustomPosition"></Button>
<Button android:layout_height="wrap_content"
android:layout_width="fill_parent" android:id="@+id/btnSimpleToastWithImage"
android:text="带图片"></Button>
<Button android:layout_height="wrap_content"
android:layout_width="fill_parent" android:text="完全自定义"
android:id="@+id/btnCustomToast"></Button>
<Button android:layout_height="wrap_content"
android:layout_width="fill_parent" android:text="其他线程"
android:id="@+id/btnRunToastFromOtherThread"></Button>
</LinearLayout>
3.custom.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:background="#ffffffff" android:orientation="vertical"
android:id="@+id/llToast" >
<TextView
android:layout_height="wrap_content"
android:layout_margin="1dip"
android:textColor="#ffffffff"
android:layout_width="fill_parent"
android:gravity="center"
android:background="#bb000000"
android:id="@+id/tvTitleToast" />
<LinearLayout
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/llToastContent"
android:layout_marginLeft="1dip"
android:layout_marginRight="1dip"
android:layout_marginBottom="1dip"
android:layout_width="wrap_content"
android:padding="15dip"
android:background="#44000000" >
<ImageView
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:id="@+id/tvImageToast" />
<TextView
android:layout_height="wrap_content"
android:paddingRight="10dip"
android:paddingLeft="10dip"
android:layout_width="wrap_content"
android:gravity="center"
android:textColor="#ff000000"
android:id="@+id/tvTextToast" />
</LinearLayout>
</LinearLayout>
分享到:
相关推荐
在 makeText 方法中,我们创建了一个 LinearLayout,添加了一个 TextView,并设置了其背景、文本颜色和文本对齐方式。然后,我们将 LinearLayout 添加到 WindowManager 中,并设置了其显示时间。 三、使用自定义 ...
在Android中,我们可以通过`Toast.makeText()`方法来创建一个`Toast`实例,并设置显示的文本: ```java Toast.makeText(context, "Hello, World!", Toast.LENGTH_SHORT).show(); ``` 现在,让我们转向自定义`Toast...
理解它们的使用方法和注意事项,对于构建高效、稳定的应用至关重要。通过“android service toast 01”这个项目,开发者可以深入学习Android后台服务的使用,以及如何在非主线程中正确地更新用户界面。
在Android SDK中,Toast类提供了show()方法来显示一个消息。通常,我们通过以下方式创建和显示一个系统Toast: ```java Toast.makeText(context, "这是一个系统Toast", Toast.LENGTH_SHORT).show(); ``` 然而,...
在Android开发中,Toast是一种轻量级的通知方式,它用于显示短暂的信息,不会阻断用户当前的操作。默认的Toast在屏幕中央短暂显示文本信息,但有时开发者可能需要更灵活的展示方式,比如改变位置或样式,这就涉及到...
然后,它会使用Android的`Toast.makeText`方法来创建并显示Toast。 `Toast.makeText`方法通常包括三个参数:上下文(Context)、要显示的文本(String)和持续时间(int)。持续时间可以是`Toast.LENGTH_SHORT`或`...
在Android应用开发中,`Toast`是常用的轻量级提示组件,用于向用户展示短暂的信息。默认情况下,`Toast`的样式和位置都是系统固定的,但开发者可以根据需求自定义其样式和显示位置,以增强用户体验。本文将详细介绍...
总的来说,`Toast`是一个轻量级的提示工具,而“类似于Android的Toast”可能是在其他平台或框架中实现的类似功能,具有相似的使用方式和扩展性,如支持图片显示。通过使用这样的组件或库,开发者可以更加灵活地设计...
以上就是`Android Toast`的基本使用方法,通过这些方式,开发者可以灵活地在应用中创建和定制`Toast`,以满足不同场景的需求。在实际开发中,结合DEMO进行实践,可以更好地理解和掌握`Toast`的用法。
在Android应用中,Menu主要用于在屏幕顶部或者底部显示一系列可选操作,通常在Activity的onCreateOptionsMenu()方法中创建: ```java @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater...
在Android SDK中,`Toast`类提供了创建和显示Toast的方法。通常,我们通过以下步骤创建一个Toast: ```java Toast.makeText(Context context, String message, int duration).show(); ``` 其中,`context`是应用的...
可以创建一个自定义`Animatable`对象,并在`show()`方法中启动动画。也可以利用属性动画库`android.animation`来实现更复杂的动画效果。 5. **多行显示** 默认的`Toast`只能显示一行文本,如果需要显示多行信息,...
在Android应用开发中,`Toast`和`Notification`是两种重要的用户反馈机制,它们用于向用户展示临时或持久的信息。让我们深入探讨这两种机制的工作原理、使用场景和实现方法。 首先,`Toast`是一种轻量级的提示方式...
实际上用户本意只是想关闭Notification,但是Toast的show方法中有调用INotificationManager这个类,而这个类在用户关闭消息通知权限的同时被禁用了,所以我们的吐司无法显示。 Toast.show() 效果图 自定义Toast...
在本文中,我们将深入探讨`Toast`的使用方法、原理以及一些实际例子。 `Toast`的基本用法非常简单,我们可以通过`Toast.makeText()`方法创建一个`Toast`实例,并传入上下文(Context)、显示文本以及持续时间...
在Android开发中,为了给用户提供即时的反馈信息,我们经常使用`Toast`类来显示短暂的通知。然而,Android原生的`Toast`功能有限,样式单一且无法自定义动画效果。针对这一问题,开发者们创建了各种扩展库,以增强`...
在Qt for Android中,我们可以通过调用`QAndroidJniObject`类来使用Java的Toast API。以下是一个简单的示例: ```cpp #include #include void showToast(const QString &message) { QAndroidJniEnvironment env...
在Android应用开发中,`Android Toast`是一种轻量级的提示机制,用于向用户显示简短的信息,通常在用户操作后出现并自动消失。标题提到的“Android Toast即便关闭了通知权限也会正常显示”是一个关键点,这涉及到...
在Android应用开发中,`Toast`有多种用法,可以满足不同的展示需求。下面将详细解释标题和描述中提到的五个`Toast`使用情形: 1. **默认效果**: 这是最基础的`Toast`使用方式,只需要调用`Toast.makeText()`方法...
在Android中,可以使用`setGravity()`方法来改变Toast的显示位置。例如,以下代码将Toast的位置设置为屏幕中央: ```java Toast toast_modify_location = Toast.makeText(getBaseContext(), "改变位置的Toast", ...