今日学习

1.了解activity生命周期

2.Toast的使用方法

(1)直接使用

 1 String ok="登录成功!";
 2 String fail="用户名或密码错误,请重新输入!";
 3 
 4 //toast普通版
 5   Toast.makeText(getApplicationContext(),ok,Toast.LENGTH_SHORT).show();
 6 
 7 //toast提升版,居中显示,无法使用
 8   Toast toastCenter = Toast.makeText(MainActivity.this,fail,Toast.LENGTH_SHORT);//没有show,因为还没有设置布局,下面是居中布局
 9   toastCenter.setGravity(Gravity.CENTER,0,0);
10   toastCenter.show();

toast居中效果没有实现,可能是因为https://blog.csdn.net/weixin_39834984/article/details/111755964

(2)封装好Toast类

 1 package com.example.tempost.util;
 2 
 3 import android.content.Context;
 4 import android.widget.Toast;
 5 
 6 public class ToastUtil {
 7     public static Toast mToast;
 8     public static void showMsg(Context context, String msg){
 9         if(mToast == null){
10             mToast = Toast.makeText(context,msg,Toast.LENGTH_SHORT);
11         }else{
12             mToast.setText(msg);
13         }
14         mToast.show();
15     }
16 }

使用封装好的方法

1 //封装好的类,二选一均可
2   ToastUtil.showMsg(getApplicationContext(),ok);
3   //ToastUtil.showMsg(MainActivity.this,ok);

原文地址:https://www.cnblogs.com/Arisf/p/14349253.html