Using findviewbyme in Android to improve component search efficiency

1. Draw out

Android beginners usually add setcontentview method to oncreate method to load layout when writing Android activity, and bind controls through findviewbyid. Only one or two components in the layout of the initial demonstration are OK, but suddenly a large number of controls need to be deployed in a layout, which will take a long time, Fortunately, Android Studio provides you with a convenient plug-in findviewbyme (you can also learn about annotations, especially at compile time, which will not affect performance).

2. Operation demonstration

During the demonstration here, I have installed the plug-in in advance. You can install it yourself, and then restart Android studio.

3. Precautions

(1) Add rootview

To add rootview to fragment, first check "add rootview";

Then enter the name of rootview in the edit box, such as contentview;

Finally, click Add to update the generated code.

(2) Viewholder in adapter

Check "is viewholder" and the code will be updated;

The control variables here no longer add any modifiers.

Some variables have to be handled by themselves, which will not be repeated here.

(3) Naming rules

Naming rules of control variables: variable naming is based on the ID of the control. For example, the ID is edit_ user_ Name, then the generated variable name is editusername;

Control variables use the private modifier by default;

For the question of adding "m", just check "add" m "in the exchange box. At this time, the variable name will become meditusername.

4. Supplement

You can also use https://github.com/boredream/BorePlugin (more refreshing)

Code generation rules:

(1) Automatically traverse all the files with ID in the target layout, and those without ID will not be recognized and processed

(2) The variable name generated by the control defaults to the ID name, which can be modified in the name input field on the right side of the pop-up confirmation box

(3) All buttons or controls with clickable = true will automatically generate setonclicklistener related code in the code

(4) All EditText controls will generate non empty judgment code in the code. If it is empty, it will prompt the hint content of EditText. If hint is empty, it will prompt that XXX string cannot be empty. Finally, it will combine the verification of all input boxes into one submit method

(5) The include label in the layout is automatically recognized and the controls in the corresponding layout are read

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. At the same time, I also hope to support a lot of programming tips!

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
分享
二维码
< <上一篇
下一篇>>