Android reminder micro tips do you really know dialog, toast and snackBar

Dialog and toast are certainly familiar to everyone. We usually use too much. SnackBar is a new control provided in the design support library. Some friends may have used it, and some friends may not understand it. But do you really know when to use dialog, toast and snackBar? In this article, we will learn the timing of the use of these three, and introduce some additional skills.

1. Dialog

First, let's introduce the usage of dialog. It's actually very simple. I believe most people often use it:

This code will pop up a very beautiful dialog, as shown in the following figure:

Now the dialog is in the material design style, because I run it on the 6.0 system, so it will be automatically given this style. But what would be the effect of running on an old version of the system, such as the 2.3 system? Just run it, as shown in the following figure:

Um.. This effect is ugly, but there is no way. This is the style of the 2.3 system at that time.

People's aesthetics is always improving. Is there any way to use the material design style dialog in the old version of the system? Of course, Google has taken this into full consideration and provided an alertdialog class in the appcompat-v7 library. The full path is:

android. support. v7. app. AlertDialog

By using the alertdialog in this package, we can keep the dialog style consistent in all system versions. Now run it again in the 2.3 system, and the effect is as follows:

It can be seen that the effect is quite good now. This is also a small skill.

The function of dialog is to give the user a prompt message and let the user make a judgment according to the prompt. The feature of dialog is that it will block the operation you are currently doing. You must stop to process the dialog. However, most people may not like to be interrupted like this. Maybe the user is dealing with an important operation and suddenly pops up a dialog to block his original operation. At this time, the user will become very angry.

Therefore, it's better to be cautious when using dialog, and try not to bring a bad sense of experience to users.

2. Toast

When it comes to not blocking the user's original operation, this extends to our second topic today, toast. Toast will only pop up a message to tell the user that something has happened and will disappear automatically after a period of time. It will not block any operation of the user at all, and even the user can ignore toast at all.

Let's take a look at the basic usage of toast, as follows:

Toast. makeText(context,"things happened",Toast.LENGTH_SHORT). show();

The last parameter is used to specify the duration of toast display, toast LENGTH_ Short indicates that the display time is short, toast LENGTH_ Long indicates that the display time is long.

However, it doesn't mean that the usage of toast has no depth at all. For example, the above writing method will have problems as shown in the figure below:

As you can see, here I quickly clicked the button five times in a row, and toast triggered five times. In fact, this experience is not good, because maybe the user shakes his hand and orders more times, resulting in that toast cannot be closed for a long time. Or we are actually doing other operations. A new toast prompt should pop up, but the last toast has not been displayed.

Therefore, the best practice is to encapsulate the toast call into an interface and write it in a public class, as shown below:

It can be seen that the way we use toast here is different from our usual way. Here, we will first judge whether the toast object is empty. If it is empty, we will call the maketext() method to generate a toast object. Otherwise, we will directly call the settext() method to set the displayed content, and finally call the show() method to display the toast. Since a new toast object will not be generated every time it is called, the problem we encountered just now will not appear here.

It is also very simple to call. You only need to pass in the context object and the content to be displayed in toast:

Now let's run the program again, and the effect is shown in the figure below:

It can be seen that no matter how many times we trigger toast calls, it will only last for the duration of toast display once. This is also a small skill.

Toast is used to tell the user what is happening now and will not block the user's operation, but at the same time, the user can only passively accept it, because there is no way for the user to choose whether to agree or refuse.

Although toast is better than dialog in terms of user experience, it should be used with caution, especially when some sensitive operations are involved. For example, when deleting data, you only give the user a prompt: "your data has been deleted", instead of giving the user the opportunity to choose whether to delete it. At this time, the user may run away.

3. Snackbar

If dialog and toast are two extremes, snackBar is in the middle. SnackBar is similar to toast, but it is more widely used, and it can interact with users. SnackBar uses an animation effect to pop up from the bottom of the screen and disappear automatically after a period of time.

Before using the snackBar, you need to start with APP / build Add corresponding dependencies to gradle:

Then you can use snackBar. Its usage is similar to toast:

Here, the make () method of snackBar is called to create a snackBar object. The first parameter of the make () method needs to pass in a view. As long as it is any view of the current interface layout, snackBar will use this view to automatically find the outermost layout for displaying snackBar. The second parameter is the content displayed in snackBar, and the third parameter is the display duration of snackBar. These are similar to toast.

Then, a setaction () method is called to set an action, so that snackBar is not only a prompt, but can interact with users. Finally, call the show () method to display the Snackbar.

Now run the program again, and the effect is shown in the following figure:

You can see that the effect of snackBar is a bit similar to toast, but it pops out from the bottom of the screen. In addition, buttons for user interaction can be added to snackBar, such as giving users an undo option when deleting data, which can improve a lot of user experience from these small details.

4. Summary

Now you have three ways to prompt users, dialog, toast and snackBar. Let's summarize the use timing of these three ways.

Dialog: use dialog when the prompt is crucial and the user must make a decision before continuing.

Toast: toast is used when the prompt just tells the user that something has happened and the user does not need to respond to it.

SnackBar: in any scenario other than the above two, snackBar may be your best choice.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>