As the saying goes, programmers who don't want to be lazy are not good programmers! So today teaches everybody to be lazy!
For example, we enter a Toast in Android development, and then we have the following quick actions:
Isn't it convenient?
A classmate asked, this is not automatic completion? Wrong, at first glance is automatic completion, not actually. Auto-completion is the completion of a method or class name, such as you enter find and then there will be a Findviewbyid method to prompt you, you can quickly locate, but we actually develop in the general need to be forced to transform it, and then add R.ID.XX to declare its ID, but you can try to enter FBC , then press tab or enter , and you'll find it more convenient than auto-completion.
This is called Live template, if you really want to translate is called real-time templates, in the as there are two templates, one is that you can create a new activity when you choose the Empty activity, fullscreen Activity and the like, this is generally for your entire file, and one is the Live Template that is introduced in this article, which can be useful in some commonly used code snippets.
Open the settings, Editor--- live Templates , you can see that there are many live Templates By default, you can look under my Android classification has the following templates:
You can enter those abbreviations to quickly enable these templates, such as defining a constant, quickly setting the View to gone, quickly starting an Activity, and so on, you can enter the const,gone, Starter to quickly operate, not feel very cool!
How is that specific? We might as well click on the starter of these template lists with the following code:
public static void start(Context context) { IntentIntent$ACTIVITY$.class); starter.putExtra($CURSOR$); context.startActivity(starter);}
which AC T I V I T Y Represents the name of the class that currently resides, CUrSOR Represents the position of the current mouse, in the same veinnewinstanceCan help you quickly declare a new Fragment method in Fragment, which has the following code:
$fragment$ newInstance($args$) { $nullChecks$ BundleBundle(); $addArgs$ $fragment$ $fragment$(); fragment.setArguments(args); return fragment;}
Where the $$ representative is a variable, wrapped in the name of the variable, you can declare the type of the variable, this later.
Isn't it easy to understand? If you understand it then you can define your own Live Templates According to your usage habits.
For example, do we often write a singleton pattern in development? Is it annoying to write such a big paragraph every time? So today we will teach you to customize a template for a singleton pattern, and then easily make the order example.
To Live Templates settings, Editor , click on the + sign in the upper right corner, select Template Group, because I am accustomed to customizing the individual groupings first, so good management, such as creating a new group called Stormzh Ang , then you will see a stormzhang group displayed in the list, when the mouse select the group, and then click on the upper right corner of the + sign, click Live Template , and then fill in the abbreviation and description, Then copy the following code into the following input box (PS: There are many ways to write a singleton pattern, so take one of these as an example)
$CLASS$ $CLASS$($CLASS$ getInstance() { synchronized ($CLASS$.class) { if (instance == null) { $CLASS$(); } } return instance;}
Note here, if your code is some fixed code, then this is the end, but this code is dynamic, there are some variables, because the class name of each class if you need to manually change it is too troublesome, so there is a variable CLAss , so you need to click on the followingDefine, first define the language range to which the variable belongs, then you can see that it supports HTML, XML, JSON, Java, C + +, etc., obviously, we need to support Java here, choose Java:
Immediately after that, we need to give the variable CLAss Definition type, here'sCLASSThe name is arbitrary, for readability, you can be happy to nameABC, the type that really gives this variable is clickedEdit Variablesbutton to edit the variable, we selectClassName ()Options, you can see that there are other options, but look at the name of the people probably guessed what the meaning, here is not explained.
Click OK to save, and a single example of the Live Template we've defined is done. You can open a class file at will, and then enter singleton Press tab or enter key to see the magical scene appeared, is not very handsome?
After reading this article to think about what you have used to the code snippet, quickly define it as a Live Template, you will find that you can become lazy again!
The original published in the public number androiddeveloper"googdev", reproduced please be sure to indicate the source!
Make you lazy Android Studio Live Templates