简体   繁体   English

有时用Snackbar代替Toast的时间确实足够长

[英]Replacing Toast with Snackbar sometimes does show for long enough

I'm in the process of replacing several Toast messages with the new Snackbar in the new Android Design Support library. 我正在用新的Android Design支持库中的新Snackbar替换几条Toast消息。

Old code: 旧代码:

Context context = getApplicationContext();
Toast.makeText(context, "Deleted...", Toast.LENGTH_LONG).show();

New code: 新代码:

View view = findViewById(android.R.id.content);
Snackbar.make(view, "Deleted...", Snackbar.LENGTH_LONG).show();

For the most part this is working fine, but I have a couple of toasts that were displayed and then it immediately destroys that activity and launches another activity. 在大多数情况下,此方法运行良好,但是我展示了几个祝酒词,然后立即破坏了该活动并启动了另一个活动。 The snackbar doesn't show up long enough on the screen, as the underlying activity and view are getting destroyed too quickly. 小吃栏在屏幕上显示的时间不够长,因为基础活动和视图被破坏得太快。

Looking for some alternative strategies to make Snackbar work similar to the Toast. 寻找一些替代策略以使Snackbar类似于Toast。

Snackbars are part of your layout. 小吃店是您布局的一部分。 If you are immediately transitioning to another activity, then you should probably stay with Toast messages or either 如果您要立即过渡到另一项活动,那么您可能应该保留Toast消息,或者

  • if you are finishing the activity and going back to a previous activity, get a result from the activity so that the newly shown activity can display the Snackbar 如果您正在完成活动并返回到上一个活动,请活动中获取结果,以便新显示的活动可以显示小吃栏
  • Send an extra or special Intent to your new Activity so that it can display a Snackbar. 向您的新活动发送额外或特殊的Intent,以便它可以显示Snackbar。

I would suggest for creating custom toast which contains ui like snack bar. 我建议创建自定义吐司,其中包含类似小吃店的ui。 For example:- https://stackoverflow.com/a/11288522/1384010 例如: -https : //stackoverflow.com/a/11288522/1384010

You can try this library. 您可以尝试此库。 This is a wrapper for android default snackbar. 这是android默认快餐栏的包装。 https://github.com/ChathuraHettiarachchi/CSnackBar https://github.com/ChathuraHettiarachchi/CSnackBar

Snackbar.with(this,null)
    .type(Type.SUCCESS)
    .message("Profile updated successfully!")
    .duration(Duration.SHORT)
    .show();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM