最近又被拉起做安卓的活了,今天碰到一个功能,要求用自定义Toast,在网上搜了一下,试了好几个,终于有一个能用了,没错,我现在想实现某个功能是需要百度然后一个一个试的,就是这么菜,好了废话不多说开始写代码
首先是新建一个类ToastUtil:
package com.hadutech.glasses.engineerapp;
import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class ToastUtil {
private Toasttoast;
private LinearLayouttoastView;
/**
* 修改原布局的Toast
*/
public ToastUtil() {
}
/**
* 完全自定义布局Toast
* @param context
* @param view
*/
public ToastUtil(Context context, View view,int duration){
toast=new Toast(context);
toast.setView(view);
toast.setDuration(duration);
}
/**
* 向Toast中添加自定义view
* @param view
* @param postion
* @return
*/
public ToastUtil addView(View view,int postion) {
toastView = (LinearLayout)toast.getView();
toastView.addView(view, postion);
return this;
}
/**
* 设置Toast字体及背景颜色
* @param messageColor
* @param backgroundColor
* @return
*/
public ToastUtil setToastColor(int messageColor,int backgroundColor) {
View view =toast.getView();
if(view!=null){
TextView message=((TextView) view.findViewById(android.R.id.message));
message.setBackgroundColor(backgroundColor);
message.setTextColor(messageColor);
}
return this;
}
/**
* 设置Toast字体及背景
* @param messageColor
* @param background
* @return
*/
public ToastUtil setToastBackground(int messageColor,int background) {
View view =toast.getView();
if(view!=null){
TextView message=((TextView) view.findViewById(android.R.id.message));
message.setBackgroundResource(background);
message.setTextColor(messageColor);
}
return this;
}
/**
* 短时间显示Toast
*/
public ToastUtil Short(Context context, CharSequence message){
if(toast==null||(toastView!=null&&toastView.getChildCount()>1)){
toast= Toast.makeText(context, message, Toast.LENGTH_SHORT);
toastView=null;
}else{
toast.setText(message);
toast.setDuration(Toast.LENGTH_SHORT);
}
return this;
}
/**
* 短时间显示Toast
*/
public ToastUtil Short(Context context,int message) {
if(toast==null||(toastView!=null&&toastView.getChildCount()>1)){
toast= Toast.makeText(context, message, Toast.LENGTH_SHORT);
toastView=null;
}else{
toast.setText(message);
toast.setDuration(Toast.LENGTH_SHORT);
}
return this;
}
/**
* 长时间显示Toast
*/
public ToastUtil Long(Context context, CharSequence message){
if(toast==null||(toastView!=null&&toastView.getChildCount()>1)){
toast= Toast.makeText(context, message, Toast.LENGTH_LONG);
toastView=null;
}else{
toast.setText(message);
toast.setDuration(Toast.LENGTH_LONG);
}
return this;
}
/**
* 长时间显示Toast
*
* @param context
* @param message
*/
public ToastUtil Long(Context context,int message) {
if(toast==null||(toastView!=null&&toastView.getChildCount()>1)){
toast= Toast.makeText(context, message, Toast.LENGTH_LONG);
toastView=null;
}else{
toast.setText(message);
toast.setDuration(Toast.LENGTH_LONG);
}
return this;
}
/**
* 自定义显示Toast时间
*
* @param context
* @param message
* @param duration
*/
public ToastUtil Indefinite(Context context, CharSequence message,int duration) {
if(toast==null||(toastView!=null&&toastView.getChildCount()>1)){
toast= Toast.makeText(context, message,duration);
toastView=null;
}else{
toast.setText(message);
toast.setDuration(duration);
}
return this;
}
/**
* 自定义显示Toast时间
*
* @param context
* @param message
* @param duration
*/
public ToastUtil Indefinite(Context context,int message,int duration) {
if(toast==null||(toastView!=null&&toastView.getChildCount()>1)){
toast= Toast.makeText(context, message,duration);
toastView=null;
}else{
toast.setText(message);
toast.setDuration(duration);
}
return this;
}
/**
* 显示Toast
* @return
*/
public ToastUtil show (){
toast.show();
return this;
}
/**
* 获取Toast
* @return
*/
public Toast getToast(){
return toast;
}
}
接着是新建一个toast_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:id="@+id/tvToastContent"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#FFC371"
android:layout_alignParentBottom="true"
android:gravity="center"
android:textColor="#FFFFFF"
android:text="输入信息有误,请仔细检查后重试。"
/>
最后就是使用了
final Handlerhandler=new Handler(){
@Override
public void handleMessage(Message msg){
switch (msg.what){
case 2:
Toast.makeText(LoginActivity.this,"网络链接异常",Toast.LENGTH_SHORT).show();
case 3:
// Toast.makeText(LoginActivity.this,codeMsg,Toast.LENGTH_SHORT).show();
// Log.d(TAG, "codeMsg:"+codeMsg);
// ToastUtil toastUtil = new ToastUtil();
// toastUtil.Short(LoginActivity.this,codeMsg).show().setToastBackground(Color.WHITE,R.drawable.toast_radius).show();
//自定义了Toast
Viewview=LayoutInflater.from(LoginActivity.this).inflate(R.layout.toast_view,null);
new ToastUtil(LoginActivity.this,view,Toast.LENGTH_SHORT).show();
}
}
};
最后的效果是这样的:
就是这样,其实也不是很难(^v^)