Advertisement

Android入门第30天-Android里的Toast的使用

阅读量:

介绍

本篇重点介绍了Android系统中提供的一种提示信息控件——Toast(吐司)! Toast作为一种便捷的消息通知框,在屏幕上显示时会立即出现,并不带有按钮。当不再获取屏幕焦点后会自行消失。 由于其应用广泛且操作简便,我们通过一个具体的案例详细讲解了其使用方法。

课程目标

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

  1. 我们采用了系统预设样式的第一种Toast;该Toast会在两秒后自行消隐;
  2. 我们采用了自定义样式的第二种Toast;该Toast将在屏幕中央呈现并持续两秒。

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

Toast.LENGTH_SHORT,2秒;

LENGTH_LONG,3点5秒;

项目结构

在定制化Toast组件中集成图片时,我们会将其放置于mipmap层级. 为了确保这一功能的有效实现,我们需要在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>
    
    
    
    
    AI助手

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>
    
    
    
    
    AI助手

后端代码

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();
    
     }
    
 }
    
    
    
    
    AI助手

动手试试吧。

全部评论 (0)

还没有任何评论哟~