Android入门之Toast的使用教程

 更新时间:2022年11月19日 09:30:56   作者:TGITCIC  
Toast是一种很方便的消息提示框,会在 屏幕中显示一个消息提示框,没任何按钮,也不会获得焦点一段时间过后自动消失!非常常用!本文就来通过一个例子把Toast的使用讲透

介绍

本篇带来的是: Android用于提示信息的一个控件——Toast(吐司)!Toast是一种很方便的消息提示框,会在 屏幕中显示一个消息提示框,没任何按钮,也不会获得焦点一段时间过后自动消失! 非常常用!我们通过一个例子把Toast的使用讲透!

课程目标

我们的目标是实现2个Toast。

  • 第一个Toast我们使用的是系统默认的样式,它显示两秒后自动消失;
  • 第二个Toast我们使用的是自定义的样式,它在屏幕的中央显示两秒后自动消失;

toast在屏幕上的闪现会有两种Duration。

  • Toast.LENGTH_SHORT,2秒;
  • LENGTH_LONG,3点5秒;

项目结构

我们会在自定义toast里使用一个图片,我们把它放在mipmap下;

我们会使用一个自定义的toast,因此需要定义一个view_toast_custom.xml文件;

前端代码

view_toast_custom.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mkToast"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
 
    <ImageView
        android:id="@+id/toastBiaoQing"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_marginLeft="10dp"
        android:src="@mipmap/wechat_goutou" />
 
    <TextView
        android:id="@+id/toastMsg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:textSize="20sp" />
 
</LinearLayout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp">
 
    <Button
        android:id="@+id/btnShowToast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="屏幕下部显示2秒toast" />
    <Button
        android:id="@+id/btnShowCustomToast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="屏幕中部显示2秒自定义toast" />
 
</LinearLayout>

后端代码

MainActivity.java

package org.mk.android.demosimpletoast;
 
import androidx.appcompat.app.AppCompatActivity;
 
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
 
public class MainActivity extends AppCompatActivity {
 
    private Button btnShowToast;
    private Button btnShowCustomToast;
    private Context ctx;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ctx = MainActivity.this;
        btnShowToast = (Button) findViewById(R.id.btnShowToast);
        btnShowToast.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "提示的内容", Toast.LENGTH_SHORT).show();
            }
        });
 
        btnShowCustomToast = (Button) findViewById(R.id.btnShowCustomToast);
        btnShowCustomToast.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                midToast("这是一个自定义的toast",Toast.LENGTH_SHORT);
            }
        });
    }
 
    private void midToast(String str, int showTime) {
        LayoutInflater inflater = getLayoutInflater();
        View view = inflater.inflate(R.layout.view_toast_custom, (ViewGroup) findViewById(R.id.mkToast));
        ImageView img_logo = (ImageView) view.findViewById(R.id.toastBiaoQing);
        TextView tv_msg = (TextView) view.findViewById(R.id.toastMsg);
        tv_msg.setText(str);
        Toast toast = new Toast(ctx);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(view);
        toast.show();
    }
}

动手试试吧。

以上就是Android入门之Toast的使用教程的详细内容,更多关于Android Toast的资料请关注脚本之家其它相关文章!

相关文章

  • Android创建一个Activity的方法分析

    Android创建一个Activity的方法分析

    这篇文章主要介绍了Android创建一个Activity的方法,结合实例形式分析了Android创建Activity的具体步骤与相关实现技巧,需要的朋友可以参考下
    2016-04-04
  • Android控件阴影颜色调整示例

    Android控件阴影颜色调整示例

    这篇文章主要介绍了Android控件阴影颜色调整示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-08-08
  • Android Canvas之drawBitmap方法案例详解

    Android Canvas之drawBitmap方法案例详解

    这篇文章主要介绍了Android Canvas之drawBitmap方法案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • Android 背景图片的缩放实现

    Android 背景图片的缩放实现

    这篇文章主要介绍了Android 背景图片的缩放实现的相关资料,需要的朋友可以参考下
    2016-11-11
  • 详解Android 7.0 Settings 加载选项

    详解Android 7.0 Settings 加载选项

    本篇文章主要介绍了Android 7.0 Settings 加载选项,Android 7.0 Settings顶部多了一个建议选项,多了个侧边栏,操作更加便捷了,有兴趣的可以了解一下。
    2017-02-02
  • Android 获取手机已安装的应用列表实现详解

    Android 获取手机已安装的应用列表实现详解

    这篇文章主要介绍了Android 获取手机已安装的应用列表的实现,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-08-08
  • Android中CountDownTimer类详解

    Android中CountDownTimer类详解

    本文详细讲解了Android中CountDownTimer类,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-12-12
  • Android中操作SQLite数据库快速入门教程

    Android中操作SQLite数据库快速入门教程

    这篇文章主要介绍了Android中操作SQLite数据库快速入门教程,本文讲解了数据库基础概念、Android平台下数据库相关类、创建数据库、向表格中添加数据、从表格中查询记录等内容,需要的朋友可以参考下
    2015-03-03
  • Android中AOP的应用实践之过滤重复点击

    Android中AOP的应用实践之过滤重复点击

    这篇文章主要给大家介绍了关于Android中AOP的应用实践之过滤重复点击的相关资料,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-09-09
  • Android实现定时任务功能

    Android实现定时任务功能

    这篇文章主要为大家详细介绍了Android实现定时任务功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01

最新评论