用系统的Toast提示,停留时间太短了,不满足某些应用的需求。有时候希望这个Toast能停留久一点,或者让这个Toast居中显示,所以,自己定义了一个Toast。直接上代码了:
package com.changewei.testvolleyandimageloader;
import java.util.Timer;
import java.util.TimerTask;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
/**
* 自定义Toast
* @author chenlin
*
*/
public class ToastView {
public static Toast toast;
private int time;
private Timer timer;
/**
* 构造函数,默认显示时间Toast.LENGTH_SHORT
* @param context
* @param text
*/
public ToastView(Context context, String text) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.toast_view, null);
TextView t = (TextView) view.findViewById(R.id.toast_text);
t.setText(text);
if(toast != null) {
toast.cancel();
}
toast = new Toast(context);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(view);
}
/**
* 构造函数,默认显示时间Toast.LENGTH_SHORT
* @param context
* @param text
*/
public ToastView(Context context, int text) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.toast_view, null);
TextView t = (TextView) view.findViewById(R.id.toast_text);
t.setText(text);
if(toast != null) {
toast.cancel();
}
toast = new Toast(context);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(view);
}
//设置toast显示位置
public void setGravity(int gravity, int xOffset, int yOffset) {
//toast.setGravity(Gravity.CENTER, 0, 0); //居中显示
toast.setGravity(gravity, xOffset, yOffset);
}
//设置toast显示时间
public void setDuration(int duration) {
toast.setDuration(duration);
}
//设置toast显示时间(自定义时间)
public void setLongTime(int duration) {
//toast.setDuration(duration);
time = duration;
timer = new Timer();
timer.schedule(new TimerTask(){
@Override
public void run() {
if(time-1000 >= 0) {
show();
time= time - 1000;
} else {
timer.cancel();
}
}
}, 0, 1000);
}
public void show() {
toast.show();
}
public static void cancel() {
if(toast != null) {
toast.cancel();
}
}
}
xml文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/toast_bg"
android:gravity="center"
android:minWidth="120dp"
android:orientation="vertical" >
<ImageView
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_margin="10dp"
android:background="@drawable/ic_launcher" />
<TextView
android:id="@+id/toast_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text=""
android:textColor="#ffffff"
android:textSize="15dp" />
</LinearLayout>
</LinearLayout>
toast_bg文件:
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#c8333333" />
<corners
android:bottomLeftRadius="6dp"
android:bottomRightRadius="6dp"
android:topLeftRadius="6dp"
android:topRightRadius="6dp" />
</shape>
最后为这个自定义Toast的使用:
ToastView toast = new ToastView(this,"欢迎使用自定义Toast");
toast.setGravity(Gravity.CENTER, 0, 0);//设置Toast的位置
toast.setLongTime(5000);//设置Toast显示的时间
toast.show();//设置Toast显示
// toast.cancel();//取消Toast
本文介绍了一种自定义Toast的方法,通过创建ToastView类,可以设置Toast的显示时间、位置等属性,并提供了示例代码。
279

被折叠的 条评论
为什么被折叠?



