Android 中 Toast 的基本用法
比较简单, 按照下面的文件复制运行即可.
Toast 是一个消息提示组件
设置显示的位置
自定义显示内容 (示例: 添加一个图片)
ToastUtil 类
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.");
}
}
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>
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>
ToastUtil 文件
package com.ykenan.alipay.util;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Looper;
import android.widget.Toast;
public class ToastUtils {
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();
}
/**
* 用于线程
*
* @param context Activity
* @param message 内容
*/
@SuppressLint("ShowToast")
public static void showShortToastLooper(Context context, String message) {
if (shortToast == null) {
Looper.prepare();
shortToast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
// 进入loop中的循环, 查看消息队列
Looper.loop();
} 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();
}
/**
* 用于线程
*
* @param context Activity
* @param message 内容
*/
@SuppressLint("ShowToast")
public static void showLongToastLooper(Context context, String message) {
if (longToast == null) {
Looper.prepare();
longToast = Toast.makeText(context, message, Toast.LENGTH_LONG);
// 进入loop中的循环, 查看消息队列
Looper.loop();
} else {
longToast.setText(message);
}
longToast.show();
}
}