简体   繁体   English

显示错误消息的最佳实践

[英]Best practice for displaying error messages

What is the best way of displaying error messages to the user? 向用户显示错误消息的最佳方法是什么?

Assume the following scenario (just for example, this question relates to common problem, when error may occur in service, in the thread etc.): 假设存在以下情况(例如,这个问题与常见问题有关,当服务,线程中可能发生错误时):

  1. We load some data for some screen 我们为某些屏幕加载一些数据
  2. Error occurs (Internet is not available, server exception, other exceptions ...) 发生错误(Internet不可用,服务器异常,其他异常...)

How to show the error? 如何显示错误? Possible solutions: 可能的解决方案:

  1. Show toasts - the simplest way but it is not the best (for many errors we'll see many toasts, even if the application works in the background) 显示敬酒-最简单的方法,但不是最好的(对于许多错误,即使应用程序在后台运行,我们也会看到很多敬酒)
  2. Show error somewhere in the screen (eg gmail shows 'No connection' at the bottom of the list and proposes to retry) 在屏幕上的某处显示错误(例如gmail在列表底部显示“无连接”并建议重试)

What is your experience? 你有什么经验? How do you show user errors? 您如何显示用户错误? Is there some guides explaining what is the best way? 是否有一些指南解释最佳方法?

I have used the alertDialog.. refer the Images. 我已经使用了alertDialog ..参考图片。 futher google it 谷歌进一步

For user Attention. 引起用户注意。

在此处输入图片说明

for form Validation edit texts use editText.setError("Your error message") method 对于表单验证编辑文本,请使用editText.setError("Your error message")方法

用于表单验证

for internet connection failed 互联网连接失败

在此处输入图片说明

for internent connection failed with retry. 的内部连接失败,重试。

在此处输入图片说明

Update 1 更新1

For showing some auto terminate info/message we use Toast for example notifying a user that your Email was sent Successfully. 为了显示一些自动终止信息/消息,我们以Toast为例,通知用户您的Email was sent Successfully. We can Use Toast like below 我们可以像下面那样使用吐司

在此处输入图片说明

Toast.makeText(context, "Email was sent Successfully.", duration).show() Toast.makeText(上下文,“电子邮件已成功发送。”,持续时间).show()

Note: User can't interact with Default toast, See also Custom Toast Layout 注意:用户无法与默认烤面包互动,另请参阅自定义烤面包布局

Another option is to use the new Snackbar 另一个选择是使用新的Snackbar

在此处输入图片说明

Hope this will be helpful 希望这会有所帮助

It depends on the app, and what the app will be able to do once it has met this error. 这取决于应用程序以及遇到此错误后应用程序将能够执行的操作。

The two methods that Google suggests in the Material Design Guide to deal with these types of messages are: Google在《 材料设计指南》中建议使用两种方法来处理这些类型的消息:

Dialogs (in this case the Alert Dialog): 对话框 (在本例中为“警告对话框”):

警报对话框

and Snackbars : 小吃店

小吃店

To use your example: Some data is requested from a remote server, but because of some error or exception, the fetch fails and no data is returned. 以您的示例为例:从远程服务器请求了一些数据,但是由于某些错误或异常,提取失败并且不返回任何数据。

At this point, the type of error message would depend on how the app will function from that point on, without that data. 此时,错误消息的类型将取决于从那时起应用程序在没有该数据的情况下将如何运行。 If the app will perform as it is, meaning the fetch was something akin to a background update, the appropriate thing to show would be a Snackbar. 如果应用程序将按原样运行,这意味着获取操作类似于后台更新,那么应该显示的是Snackbar。 Why? 为什么?

From the Guide: 从指南:

Snackbars provide lightweight feedback about an operation by showing a brief message at the bottom of the screen. 小吃栏通过在屏幕底部显示简短消息来提供有关操作的轻量级反馈。 Snackbars can contain an action. 小吃店可以包含一个动作。

Lightweight is really the reason here. 轻巧确实是这里的原因。 If the app will function without that background data fetch, you should not block the UI with a message. 如果应用程序在没有获取背景数据的情况下仍能正常运行,则不应使用消息阻止UI。 Just let the user know things didn't work out the way they should so that he can do something about it if he cares. 只是让用户知道事情并没有按照他们应有的方式进行,这样他就可以在需要时做些事情。

Here is an example taken from the guide : 这是来自指南的示例:

在此处输入图片说明

For code: the Developer Docs on Snackbars 对于代码: Snackbars上Developer Docs

Never use a Toast. 切勿使用烤面包。 A Toast is too small, too brief and can go by unnoticed. 祝酒词太小,太简短,可能会被忽略。 Use a Snackbar. 使用小吃店。

But , in the scenario where your app will not function, or will show nothing but a blank screen without that data, the correct thing to do would be to show an Alert Dialog. 但是 ,在您的应用无法运行或仅显示没有该数据的空白屏幕的情况下,正确的做法是显示“警报对话框”。

No one wants to see nothing but a blank screen, and if you can't populate it with data, you need to give the user a screen from which they can perform alternate functions, even if that is to quit the app. 除了空白屏幕,没有人希望看到任何东西,并且如果您无法在其中填充数据,则需要为用户提供一个屏幕,用户可以从中执行其他功能,即使要退出该应用程序也是如此。

From the Guide on Alerts: 从警报指南中:

Alerts inform the user about a situation or action that requires their confirmation or acknowledgement before proceeding. 警报会通知用户有关需要进行确认或确认的情况或操作。 They differ slightly in appearance based upon the severity and impact of the message conveyed. 根据所传达消息的严重性和影响,它们的外观略有不同。

Alerts are interruptive, urgent, and prevent users from proceeding until they make a decision. 警报是中断性的,紧急的,并且会阻止用户继续进行操作直到他们做出决定。

AND

Disambiguation from Snackbars: In contrast to Alerts, Snackbars present optional but important information or actions and usually appear after an action. 与Snackbars消除歧义:与Alerts相比,Snackbars提供可选但重要的信息或操作,通常显示在操作之后。 For example, use an alert to confirm discarding a draft. 例如,使用警报来确认丢弃草稿。 Use a snackbar to present an undo action, because the action is optional and the user can continue with their primary task without taking action. 使用小吃栏来呈现撤消操作,因为该操作是可选的,并且用户可以继续其主要任务而无需执行任何操作。

So if the app won't function without that data, go with an Alert Dialog . 因此,如果没有该数据应用程序将无法运行,请使用“ 警告对话框”

Anything but a Toast. 除了吐司什么都没有。 Check out Croutons: http://android.cyrilmottier.com/?p=773 查看油煎面包块: http : //android.cyrilmottier.com/? p=773

I would say that it depends on wether your application currently has a visible active activity or not. 我要说的是,这取决于您的应用程序当前是否具有可见的活动。 If it does you could use any of the techniques already suggested without confusing the user about context etc. 如果可以的话,您可以使用已经建议的任何技术,而不会使用户对上下文等感到困惑。

If the error/message originates from background code eg a service, and your application isn't active, a notification is a good alternative. 如果错误/消息源自后台代码(例如服务),而您的应用程序未处于活动状态,则通知是一个不错的选择。 Also, take a look at the guidelines/patterns described on the developer site. 另外,请查看开发者网站上介绍的准则/模式。

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

相关问题 在可滚动列表中显示多个位图的最佳实践 - Best practice for displaying multiple bitmaps in a scrollable list Android RecyclerView 显示项目的最佳实践 - Android RecyclerView best practice for displaying items 在Android中将消息从GLSurfaceView传递到MainActivity的最佳实践? - Best practice for passing messages from GLSurfaceView to MainActivity in Android? 如何从AsynTask发送事件消息(最佳实践)? - How to send event messages from AsynTask (best practice)? 在Android中显示高大而丰富的内容的最佳做法。 ScrollView还是ListView? - Best practice for displaying tall, rich content in Android. ScrollView or ListView? 在 Android 应用程序中显示 Facebook 配置文件图片的最佳实践 - Best Practice For Displaying Facebook Profile Pictures In an Android App 在原生Android应用程序的地图上显示KML图层最佳实践 - Displaying KML Layers on Maps at Native Android apps Best Practice 存储和显示许多Android可绘制资源的最佳实践/方法 - Best practice/method for storing and displaying many Android drawable resources 在 RecyclerView.ViewHolder 中显示列表的最佳实践? - Best practice for displaying list inside RecyclerView.ViewHolder? 处理HTTP错误调用的最佳实践 - Best practice for handling HTTP error call
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM