UI弹出组件Toast
对于Toast,之前并不陌生,因为之前在测试的时候大量使用过,这次来深入展开对Toast的了解和学习。自己还跟着封装了一下。。。。
大概分一下类
Toast大概分为三种。
- 普通Toast
- 改变位置的Toast(其实两者本质是一种)
- 自定义Toast
代码部分
我们先做一个按钮的界面,方便测试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"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_toast_1"
android:text="默认"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_toast_2"
android:text="改变位置"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_toast_3"
android:text="带图片(自定义)"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_toast_4"
android:text="ToastUtil"
android:textAllCaps="false"/>
</LinearLayout>
一个简单的界面,大概是这样的
自定义Toast界面,也很简单,没加内容,代码内设置
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#99000000"
android:gravity="center">
<LinearLayout
android:layout_width="200dp"
android:layout_height="200dp"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/iv_toast"
android:scaleType="fitCenter"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_toast"
android:textSize="18sp"
android:textColor="#FFFFFF"
android:layout_marginTop="10dp"/>
</LinearLayout>
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.myapplication.util.ToastUtil;
public class ToastActivity extends AppCompatActivity {
//声明控件
private Button mBtnToast1,mBtnToast2,mBtnToast3,mBtnToast4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//找到控件
setContentView(R.layout.activity_toast);
mBtnToast1 = findViewById(R.id.btn_toast_1);
mBtnToast2 = findViewById(R.id.btn_toast_2);
mBtnToast3 = findViewById(R.id.btn_toast_3);
mBtnToast4 = findViewById(R.id.btn_toast_4);
OnClick onClick = new OnClick();
mBtnToast1.setOnClickListener(onClick);
mBtnToast2.setOnClickListener(onClick);
mBtnToast3.setOnClickListener(onClick);
mBtnToast4.setOnClickListener(onClick);
}
//封装一个内部类设置点击事件,算是一种封装吧
class OnClick implements View.OnClickListener{
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_toast_1:
//这里是个普通Toast
Toast.makeText(getApplicationContext(),"Toast",Toast.LENGTH_LONG).show();
break;
case R.id.btn_toast_2:
Toast toastCenter = Toast.makeText(getApplicationContext(),"居中Toast",Toast.LENGTH_LONG);
//setGravity方法就可以调整Toast位置了,注意参数
toastCenter.setGravity(Gravity.CENTER,0,0);
toastCenter.show();
break;
case R.id.btn_toast_3:
//这是自定义的一个Toast
Toast toastCustom = new Toast(getApplicationContext());
//先用LayoutInflater找到我们自定义的界面
LayoutInflater inflater = LayoutInflater.from(ToastActivity.this);
View view = inflater.inflate(R.layout.layout_toast,null);
//再把相关控件找到
ImageView imageView = view.findViewById(R.id.iv_toast);
TextView textView = view.findViewById(R.id.tv_toast);
//setImageResource方法就可以把我们在网上找的一个icon图片传给里面的ImageView
imageView.setImageResource(R.drawable.icon_smile);
textView.setText("自定义Toast");
toastCustom.setView(view);
//setDuration方法设置Toast弹出的时间
toastCustom.setDuration(Toast.LENGTH_LONG);
toastCustom.show();
break;
case R.id.btn_toast_4:
ToastUtil.showMsg(getApplicationContext(),"包装过的Toast");
break;
}
}
}
}
出来的效果是:
测试的时候发现,点击多个Toast,所有Toast会一个一个排队显示,点个十次就要花十次的时间,一般我们是不希望这样的。(当然这跟安卓本身的API有关,高版本的就不会出现这种情况)
所以我跟着写了一个类封装了一下Toast的方法,这样点十次,还是只弹出一次的时间。大概意思看标注,这个还是挺有用的
package com.example.myapplication.util;
import android.content.Context;
import android.widget.Toast;
public class ToastUtil {
public static Toast mToast;
public static void showMsg(Context context,String msg){
//如果当前没有Toast,那就Toast出去一个
if (mToast == null){
mToast = Toast.makeText(context,msg,Toast.LENGTH_LONG);
}
else
//否则Toast这一个
mToast.setText(msg);
mToast.show();
}
}