【发布时间】:2014-05-13 05:25:44
【问题描述】:
我是 android 新手,我想在 Toast 中看到我的消息,但它会显示一段时间。
例如,我想将消息显示到一小时或更长时间。
【问题讨论】:
-
为此使用 AlertDialog。
标签: android android-toast
我是 android 新手,我想在 Toast 中看到我的消息,但它会显示一段时间。
例如,我想将消息显示到一小时或更长时间。
【问题讨论】:
标签: android android-toast
不,你不能。使用自定义对话框并在需要时将其关闭。但是我想知道你为什么要这么长时间显示某种弹出窗口。
我建议重新考虑您的设计。
您可能还想检查面包丁
【讨论】:
尝试使用对话框而不是 toast
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 的值是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();
}
【讨论】:
Toast.LENGTH_LONG 是最大值。不能超过这个
好吧,就像这里说的那样,没有正确的方法可以做到这一点。
但是,有一种破解方法 - 只需在 for-loop 中运行您的 Toast,迭代次数将控制长度。例如 - 运行循环两次(如下所示)将使时间加倍。运行 3 次将使长度增加三倍。同样,这只是一种可行的解决方法:-)
for (int i=0; i < 2; i++)
{
Toast.makeText(this, "test", Toast.LENGTH_LONG).show();
}
您必须考虑到它确实存在缺陷 - 如果用户在循环结束之前退出应用程序,它将继续显示,并且在某些设备上,Toast 可能会在每次迭代之间闪烁。所以,由你决定!
【讨论】:
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);
}
});
}
}
【讨论】:
将 toast 设置为以毫秒为单位的特定时间段:
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);
}
}
}
【讨论】: