Utilisation de base de Toast sous Android

Utilisation de base de Toast sous Android

C'est relativement simple, il suffit de suivre la copie du fichier et de l'exécuter.

Toast est un composant d'invite de message.
Définissez la position d'affichage.
Personnalisez le contenu de l'affichage (exemple: ajouter une image)
Classe ToastUtil

Fichier ToastActivity

package com.example.hello;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import com.example.hello.util.ToastUtil;

public class ToastActivity extends AppCompatActivity {
    
    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_toast);
    }

    /**
     * 默认显示
     *
     * @param view view
     */
    public void showToastOne(View view) {
    
    
        Toast.makeText(getApplicationContext(), "Toast", Toast.LENGTH_SHORT).show();
    }

    /**
     * 居中显示
     *
     * @param view view
     */
    public void showToastTwo(View view) {
    
    
        Toast makeText = Toast.makeText(getApplicationContext(), "居中 Toast", Toast.LENGTH_SHORT);
        makeText.setGravity(Gravity.CENTER, 0, 0);
        makeText.show();
    }

    /**
     * 加载照片显示
     *
     * @param view view
     */
    @SuppressLint("InflateParams")
    public void showToastThree(View view) {
    
    
        Toast makeText = new Toast(getApplicationContext());
        LayoutInflater layoutInflater = LayoutInflater.from(ToastActivity.this);
        View inflate = layoutInflater.inflate(R.layout.layout_toast, null);
        makeText.setView(inflate);
        makeText.show();
    }

    /**
     * 抵消显示
     *
     * @param view view
     */
    public void showToastFour(View view) {
    
    
        ToastUtil.showShortToast(getApplicationContext(), "多次点击进行抵消, 封装的 Toast.");
    }
}

fichier activity_toast

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".ToastActivity">

    <Button
        android:id="@+id/btn_toast_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/toastDefault"
        android:onClick="showToastOne" />

    <Button
        android:id="@+id/btn_toast_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/toastChangePosition"
        android:onClick="showToastTwo" />

    <Button
        android:id="@+id/btn_toast_3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/toastImage"
        android:onClick="showToastThree" />

    <Button
        android:id="@+id/btn_toast_4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/toastUtil"
        android:onClick="showToastFour" />

</LinearLayout>

fichier layout_toast

<?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">

    <ImageView
        android:id="@+id/toast_iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/beautiful"
        android:scaleType="centerCrop"
        android:src="@mipmap/beautiful" />

</LinearLayout>

Fichier ToastUtil

package com.example.hello.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.widget.Toast;

public class ToastUtil {
    
    

    private static Toast shortToast;
    private static Toast longToast;

    @SuppressLint("ShowToast")
    public static void showShortToast(Context context, String message) {
    
    
        if (shortToast == null) {
    
    
            shortToast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
        } else {
    
    
            shortToast.setText(message);
        }
        shortToast.show();
    }

    @SuppressLint("ShowToast")
    public static void showLongToast(Context context, String message) {
    
    
        if (longToast == null) {
    
    
            longToast = Toast.makeText(context, message, Toast.LENGTH_LONG);
        } else {
    
    
            longToast.setText(message);
        }
        longToast.show();
    }

}

Insérez la description de l'image ici

Je suppose que tu aimes

Origine blog.csdn.net/YKenan/article/details/112984109
conseillé
Classement