public class MessageBar extends Toast
{
private static MessageBar mToast;
private static View v;
public MessageBar(Context context)
{
super(context);
}
/**
* 1:环境 2:字符串 3:延迟大小
*/
// public static void show(Context context, String message, int duration)
// {
// initView(context, message, duration).show();
// }
/**
* 1:环境 2:字符串id 3:延迟大小
*/
// public static void show(Context context, int stringId, int duration)
// {
// initView(context, context.getResources().getString(stringId), duration).show();
// }
/**
* 解决连续弹出toast的问题
*/
public static void show(Context context, String message)
{
if (null == context || null == message || "".equals(message))
{
return;
}
if (mToast == null)
{
mToast = initView(context, message, Toast.LENGTH_SHORT);
}
else
{
mToast.setText(message);
}
mToast.setGravity(Gravity.FILL_HORIZONTAL | Gravity.BOTTOM, 0, 0);
mToast.show();
}
/**
* setText
*/
public void setText(CharSequence s)
{
if (v == null)
{
throw new RuntimeException(
"This Toast was not created with Toast.makeText()");
}
TextView tv = (TextView) v.findViewById(R.id.mbMessage);
if (tv == null)
{
throw new RuntimeException(
"This Toast was not created with Toast.makeText()");
}
tv.setText(s);
}
/**
* 初始化toast 视图
*/
public static MessageBar initView(Context context, CharSequence text,
int duration)
{
MessageBar result = new MessageBar(context);
LayoutInflater inflate = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflate.inflate(R.layout.mb_messagebar, null);
TextView tv = (TextView) v.findViewById(R.id.mbMessage);
tv.setText(text);
result.setView(v);
result.setDuration(duration);
return result;
}
}
{
private static MessageBar mToast;
private static View v;
public MessageBar(Context context)
{
super(context);
}
/**
* 1:环境 2:字符串 3:延迟大小
*/
// public static void show(Context context, String message, int duration)
// {
// initView(context, message, duration).show();
// }
/**
* 1:环境 2:字符串id 3:延迟大小
*/
// public static void show(Context context, int stringId, int duration)
// {
// initView(context, context.getResources().getString(stringId), duration).show();
// }
/**
* 解决连续弹出toast的问题
*/
public static void show(Context context, String message)
{
if (null == context || null == message || "".equals(message))
{
return;
}
if (mToast == null)
{
mToast = initView(context, message, Toast.LENGTH_SHORT);
}
else
{
mToast.setText(message);
}
mToast.setGravity(Gravity.FILL_HORIZONTAL | Gravity.BOTTOM, 0, 0);
mToast.show();
}
/**
* setText
*/
public void setText(CharSequence s)
{
if (v == null)
{
throw new RuntimeException(
"This Toast was not created with Toast.makeText()");
}
TextView tv = (TextView) v.findViewById(R.id.mbMessage);
if (tv == null)
{
throw new RuntimeException(
"This Toast was not created with Toast.makeText()");
}
tv.setText(s);
}
/**
* 初始化toast 视图
*/
public static MessageBar initView(Context context, CharSequence text,
int duration)
{
MessageBar result = new MessageBar(context);
LayoutInflater inflate = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflate.inflate(R.layout.mb_messagebar, null);
TextView tv = (TextView) v.findViewById(R.id.mbMessage);
tv.setText(text);
result.setView(v);
result.setDuration(duration);
return result;
}
}