自定义的Toast
建议大家花10分钟到半小时左右的时间看一下Toast
的源码
1.需求问题
平时使用的Toast并不能满足用户的需求, 我们可以使用自定义的Toast, 这种自定义的Toast能够覆盖已经执行了的Toast语句,但还没有在屏幕上消失。 意思就是系统自带的Toast,它是以队列的形式输出的, 必须要等上一个Toast 语句消失后, 才会显示下一个Toast语句, 而自定义的没有这种限制条件。话不多说, 直接上代码。
2.需要用到的代码
(1) . 需要一个shape图形(状态列表图形),名称为toast_backgound.xml
这是自定义Toast的背景,便于显示,可以用黑底白字, 或者白底黑字,我这里所使用的是黑底白字。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="10dp"/>
<solid android:color="#CC000000"/>
</shape>
注:一些需要注意的地方我都做了注释说明
(2) . 需要自定义一个BToast继承Toast类,名称为BToast.java
。
package com.example.myapplication;
import android.content.Context;
import android.text.Layout;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class BToast extends Toast {
private BToast (Context context) {
super(context);
}
private static BToast toast;
private static void cancelToast() {
//为什么把这个if语句去掉,就会异常终止呢?
//因为程序第一次进来时, toast时为空的,第二次及以后, 它才不为空,才能执行toast.cancel()。
if (toast != null) {
toast.cancel();
}
}
public void cancel() {
super.cancel();
}
public void show() {
super.show();
}
public static void initToast( Context context, CharSequence text) {
//第二次点击及以后,它会先取消上一次的Toast, 然后show本次的Toast。
cancelToast();
toast = new BToast(context);
View mView = LayoutInflater.from(context).inflate(R.layout.activity_new_toast, null);
TextView mText = mView.findViewById(R.id.toast_show);
mText.setText(text);
toast.setView(mView);
toast.setGravity(Gravity.BOTTOM, 0, 70);
toast.setDuration(Toast.LENGTH_LONG);
toast.show();
}
}
(3). 实现BToast类还需要一个布局文件,名称为activity_new_toast.xml
文件。这个TextView控件是用来显示输出的Toast中的文字。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/toast_background"
>
<TextView
android:id="@ id/toast_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:textColor="#FFFFFF"
android:gravity="center"
android:textSize="17sp" />
</LinearLayout>
(4). 主程序,用来测试BToast类,名称为MainActivity.java
.
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button).setOnClickListener(this);
findViewById(R.id.button1).setOnClickListener(this);
}
public void onClick(View view) {
if(view.getId() == R.id.button) {
BToast.initToast(this, "你点击了左边");
} else if (view.getId() == R.id.button1) {
BToast.initToast(this,"你选择的是右边");
}
}
}
(5) . 对应的布局代码,名称为activity_main.xml
.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:gravity="center">
<Button
android:id="@ id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="左边" />
<Button
android:id="@ id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@ id/button"
android:text="右边"
/>
</RelativeLayout>
3. 以上就是自定义Toast所需要的代码文件了。
点击按钮即可!!!
Good luck!!!