[英]How to display new Toast that overrides old Toast in no time in android
[英]How to display Android toast for longer time
我是android的新手,我想在Toast
看到我的消息,但是显示了一段时间。
我想显示该消息,例如直到一小时或更长时间。
不,你不能。 使用“自定义对话框”,然后在需要时将其关闭。 但我想知道为什么您要显示这么长时间的弹出窗口。
我建议重新考虑您的设计。
您可能还需要检查Crouton
尝试使用对话框代替吐司
SingleButtton.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// Creating alert Dialog with one Button
AlertDialog alertDialog = new AlertDialog.Builder(AlertDialogActivity.this).create();
// Setting Dialog Title
alertDialog.setTitle("Alert Dialog");
// Setting Dialog Message
alertDialog.setMessage("Welcome to Android Application");
// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.tick);
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which)
{
// Write your code here to execute after dialog closed
}
});
// Showing Alert Message
alertDialog.show();
}
});
LENGTH_SHORT and LENGTH_LONG
的值LENGTH_SHORT and LENGTH_LONG
为0 and 1
它们被视为标志,因此我认为除此以外无法设置时间。
您可以尝试以下方法:
编辑:
int time = 1000*60 // 1 hour
for (int i=0; i < time; i++)
{
Toast.makeText(this, "Your msg", Toast.LENGTH_LONG).show();
}
好吧,就像这里所说的,没有正确的方法来做到这一点。
但是,这有种技巧-只需在for-loop
运行Toast即可,迭代次数将控制长度。 例如,两次运行循环(如下所示)将使时间加倍。 运行3次将使长度增加三倍。 再说一次,这只是一种解决方法:-)
for (int i=0; i < 2; i++)
{
Toast.makeText(this, "test", Toast.LENGTH_LONG).show();
}
您必须考虑到它确实存在缺陷-用户会在循环结束之前继续退出应用程序,然后继续显示该应用程序,并且在某些设备上,Toast可能在每次迭代之间闪烁。 因此,取决于您!
吐司的目的是一次显示一条简单的消息。 您不能长时间显示它。 您可以使用对话框为Toast消息自定义自己的UI。
public static void showCustomToast(final Activity mActivity,final String helpText,final int sec) {
if(mActivity != null){
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
int mSec = 3000;
if(sec != 0){
mSec = sec;
}
LayoutInflater inflater = mActivity.getLayoutInflater();
View messageDialog = inflater.inflate(R.layout.overlay_message, null);
layer = new CustomLayout(mActivity);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
messageDialog.setLayoutParams(params);
TextView message = (TextView) messageDialog.findViewById(R.id.messageView);
Button okBtn = (Button) messageDialog.findViewById(R.id.messageOkbtn);
if(okBtn != null){
okBtn.setVisibility(View.GONE);
}
message.setText(helpText);
final Dialog dialog = new Dialog(mActivity,R.style.ThemeDialogCustom);
dialog.setContentView(messageDialog);
dialog.show();
final Timer t = new Timer();
t.schedule(new TimerTask() {
@Override
public void run() {
if(dialog.isShowing()){
dialog.dismiss();
}
t.cancel();
}
},mSec);
}
});
}
}
将烤面包设置为特定的时间(以毫秒为单位):
public void toast(int millisec, String msg) {
Handler handler = null;
final Toast[] toasts = new Toast[1];
for(int i = 0; i < millisec; i+=2000) {
toasts[0] = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
toasts[0].show();
if(handler == null) {
handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
toasts[0].cancel();
}
}, millisec);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.