总是感觉项目中使用系统封装好toast样式太丑,所以决定自己写一个
1.工具类
package mytoast.example.mytoast;
import android.content.Context;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import android.os.CountDownTimer;
/**
* Created by jiawei on 2018/8/6.
*/
public class ToastUtils {
private Toast mToast;
private TextView mTextView;
private String message;
private Handler mHandler = new Handler();
private boolean canceled = true;
private TimeCount timeCount;
public ToastUtils(Context context, int layoutId, String msg) {
message = msg;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//自定义布局
View view = inflater.inflate(layoutId, null);
//自定义toast文本
mTextView = (TextView) view.findViewById(R.id.toast_msg);
mTextView.setText(msg);
Log.i("ToastUtil", "Toast start...");
if (mToast == null) {
mToast = new Toast(context);
Log.i("ToastUtil", "Toast create...");
}
//设置toast居中显示
mToast.setGravity(Gravity.CENTER, 0, 0);
mToast.setDuration(Toast.LENGTH_SHORT);
mToast.setView(view);
}
/**
* 自定义居中显示toast
*/
public void show() {
mToast.show();
Log.i("ToastUtil", "Toast show...");
}
/**
* 自定义时长、居中显示toast
* @param duration
*/
public void show(int duration) {
timeCount = new TimeCount(duration, 1000);
Log.i("ToastUtil", "Toast show...");
if (canceled) {
timeCount.start();
canceled = false;
showUntilCancel();
}
}
/**
* 隐藏toast
*/
private void hide() {
if (mToast != null) {
mToast.cancel();
}
canceled = true;
Log.i("ToastUtil", "Toast that customed duration hide...");
}
private void showUntilCancel() {
if (canceled) { //如果已经取消显示,就直接return
return;
}
mToast.show();
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
Log.i("ToastUtil", "Toast showUntilCancel...");
showUntilCancel();
}
}, Toast.LENGTH_LONG);
}
/**
* 自定义计时器
*/
private class TimeCount extends CountDownTimer {
public TimeCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval); //millisInFuture总计时长,countDownInterval时间间隔(一般为1000ms)
}
@Override
public void onTick(long millisUntilFinished) {
mTextView.setText(message + ": " + millisUntilFinished / 1000 + "s后消失");
}
@Override
public void onFinish() {
hide();
}
}
}
2.使用
package mytoast.example.mytoast;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements View.OnClickListener{
private Context context = MainActivity.this;
private Button btDefaultToast;
private Button btCenterToast;
private Button btTopToast;
private Button btCenterImgToast1;
private Button btCenterImgToast2;
private Button btCustomDurationToast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initListener();
}
private void initView() {
btDefaultToast = (Button)findViewById(R.id.bt_default_toast);
btCenterToast = (Button)findViewById(R.id.bt_center_toast);
btTopToast = (Button)findViewById(R.id.bt_top_toast);
btCenterImgToast1 = (Button)findViewById(R.id.bt_center_img_toast1);
btCenterImgToast2 = (Button)findViewById(R.id.bt_center_img_toast2);
btCustomDurationToast = (Button)findViewById(R.id.bt_custom_duration_toast);
}
private void initListener() {
btDefaultToast.setOnClickListener(this);
btCenterToast.setOnClickListener(this);
btTopToast.setOnClickListener(this);
btCenterImgToast1.setOnClickListener(this);
btCenterImgToast2.setOnClickListener(this);
btCustomDurationToast.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_default_toast:
Toast.makeText(context, "默认toast", Toast.LENGTH_SHORT).show();
break;
case R.id.bt_center_toast:
Toast toast = Toast.makeText(context, "自定义居中toast", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
break;
case R.id.bt_top_toast:
Toast toast1 = Toast.makeText(context, "自定义顶部toast", Toast.LENGTH_SHORT);
toast1.setGravity(Gravity.TOP, 0, 0);
toast1.show();
break;
case R.id.bt_center_img_toast1:
ToastUtils toastUtil = new ToastUtils(context, R.layout.toast_center, "完全自定义居中toast 1");
toastUtil.show();
break;
case R.id.bt_center_img_toast2:
ToastUtils toastUtil2 = new ToastUtils(context, R.layout.toast_center_horizontal, "完全自定义居中toast 2");
toastUtil2.show();
break;
case R.id.bt_custom_duration_toast:
ToastUtils toastUtil3 = new ToastUtils(context, R.layout.toast_center_horizontal, "带图片自定义时长toast");
toastUtil3.show(5000);
break;
default:
break;
}
}
}
3.展示