Toast提示

 

Toast用于向用户显示一些帮助/提示。下面我做了5中效果,来说明Toast的强大,定义一个属于你自己的Toast。

注意:

LENGTH_LONG---长时间显示视图或文本提示

LENGTH_SHORT---短时间显示视图或文本提示

setGravity(int gravity,int xOffset,int yOffset)---设置提示应该在屏幕上的显示的位置

setDuration(int duartion)---设置提示显示的持续时间

 

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>

<think>好的,用户的问题是在Android中如何实现Toast提示功能。首先,我需要回顾一下Toast的基本用法,然后看看是否有更高级的自定义方法,可能还需要考虑不同引用中的信息。 首先,用户提到的引用[2]详细介绍了Toast的基本使用和自定义方法,应该从这里入手。基本用法应该是使用Toast.makeText(),然后调用show()。这是最常见的做法,应该作为第一步回答。 接下来,用户可能想知道如何自定义Toast的位置、样式等。引用[2]中提到可以设置位置为中间,修改字体颜色和加粗。这时候需要提到setGravity方法,以及如何通过自定义布局来实现更复杂的效果。可能需要提到LayoutInflater来加载自定义的XML布局,然后设置到Toast中。 另外,引用[3]虽然讲的是QT/qml实现类似Toast的功能,但可能对Android开发者来说,知道其他平台的实现方式可能也有帮助,不过用户的问题集中在Android,所以这部分可能暂时不需要涉及,除非用户有跨平台需求,但当前问题中没有提到。 还要注意用户可能遇到的常见问题,比如在哪里调用Toast,是否需要主线程等。例如,在子线程中直接调用Toast会导致崩溃,需要提醒用户使用runOnUiThread或者Handler来在主线程显示。 另外,是否需要提到不同Android版本的差异?比如,在较新的版本中,Toast的默认样式是否有变化,或者是否有新的API。不过根据引用内容,可能没有提到这些,所以暂时先不涉及,除非确定有相关信息。 可能还需要提到Toast的持续时间,比如LENGTH_SHORT和LENGTH_LONG的区别,这也是基本用法的一部分。 总结回答结构应该是:先介绍基本用法,然后自定义方法,最后注意事项和常见问题。同时要确保使用正确的中文术语,代码示例要符合格式要求,行内公式用$...$,独立公式用$$...$$,但这个问题可能不涉及数学公式,所以可能不需要。代码块需要用python的格式吗?用户提供的示例中是Python,但Android开发使用的是Java或Kotlin,需要确认。根据引用中的代码示例,比如引用[2]中的代码可能是Java,所以回答中的代码示例应该用Java,并且正确标注代码语言。 另外,用户可能需要生成相关问题,根据Toast的实现方式,相关问题可能包括如何自定义样式,如何在不同线程中使用,Toast和其他提示方式的区别等。 最后,引用标识需要正确对应,引用[2]是关于Android自定义Toast的,所以相关部分末尾添加[^2],而基本用法可能来自通用知识,但引用[2]也提到了基本使用,所以可能需要标记。 现在开始组织回答: 1. 基本使用方法:使用Toast.makeText().show(),解释参数,代码示例用Java。 2. 自定义位置:使用setGravity方法,示例代码。 3. 自定义布局:使用setView,加载自定义XML,示例代码。 4. 注意事项:主线程、持续时间等。 5. 相关问题:生成三个相关问题,如自定义样式、线程处理、与其他提示对比。 需要确保所有步骤清晰,代码正确,引用正确标注。例如,在讲自定义布局时,引用[2]提到了相关内容,所以在末尾添加。</think>### 如何在Android中实现Toast提示功能? ToastAndroid开发中常用的轻量级消息提示组件,能够在不打断用户操作的情况下显示信息并自动消失。以下是实现Toast的详细步骤: --- #### **1. 基本使用方法** 使用系统默认样式显示Toast,代码示例如下: ```java // 在Activity中调用 Toast.makeText(this, "这是一条默认Toast", Toast.LENGTH_SHORT).show(); ``` - **参数说明**: - 第一个参数:上下文(通常为当前Activity)。 - 第二个参数:要显示的文本内容。 - 第三个参数:持续时间(`Toast.LENGTH_SHORT`约2秒,`Toast.LENGTH_LONG`约3.5秒)[^2]。 --- #### **2. 自定义Toast位置** 默认情况下Toast显示在屏幕底部,可通过`setGravity()`调整位置: ```java Toast toast = Toast.makeText(this, "居中显示的Toast", Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 0, 0); // 设置居中显示 toast.show(); ``` - `Gravity.CENTER`表示居中,后两个参数为水平和垂直偏移量。 --- #### **3. 自定义布局与样式** 通过XML布局文件实现更复杂的样式(如修改背景、字体颜色等): 1. **定义布局文件**(`custom_toast.xml`): ```xml <LinearLayout android:id="@+id/custom_toast_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#333" android:padding="16dp"> <TextView android:id="@+id/toast_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFF" android:textSize="18sp" android:textStyle="bold"/> </LinearLayout> ``` 2. **加载布局并显示**: ```java LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.custom_toast, findViewById(R.id.custom_toast_layout)); TextView text = layout.findViewById(R.id.toast_text); text.setText("自定义样式Toast"); Toast toast = new Toast(this); toast.setDuration(Toast.LENGTH_LONG); toast.setView(layout); // 绑定自定义布局 toast.show(); ``` 此方法可设置背景色、字体加粗、居中对齐等。 --- #### **4. 注意事项** - **主线程调用**:Toast必须在主线程中显示,子线程中需通过`runOnUiThread`或`Handler`切换。 - **避免频繁触发**:连续调用可能导致Toast队列过长,影响用户体验。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值