How to automatically generate getters and setters in Android Studio

后端 未结 15 1144
太阳男子
太阳男子 2020-12-02 04:15

Is there a shortcut in Android Studio for automatically generating the getters and setters in a given class?

相关标签:
15条回答
  • 2020-12-02 05:08

    You can use AndroidAccessors Plugin of Android Studio to generate getter and setter without m as prefix to methods

    Ex: mId; Will generate getId() and setId() instead of getmId() and setmId()

    0 讨论(0)
  • 2020-12-02 05:08

    Another funny way

    Type the parameter name anywhere in the object after definition, you will see setter and getter, Just select and click enter :)

    I tried with Android Studio 2.3

    0 讨论(0)
  • 2020-12-02 05:09

    Right click on Editor then Select Source -> Generate Getters and Setters or press Alt + Shift + S enter image description here

    0 讨论(0)
  • 2020-12-02 05:11

    for macOS, +N by default.

    Right-click and choose "Generate..." to see current mapping. You can select multiple fields for which to generate getters/setters with one step.

    See http://www.jetbrains.com/idea/webhelp/generating-getters-and-setters.html

    0 讨论(0)
  • 2020-12-02 05:12

    Android Studio & OSx :

    Press cmd+n > Generate > Getter and Setter

    Android Studio & Windows :

    Press Alt + Insert > Generate > Getter and Setter

    0 讨论(0)
  • 2020-12-02 05:14

    You can generate getter and setter by following steps:

    • Declare variables first.
    • click on ALT+Insert on keyboard placing cursor down to variable declaration part
    • now select constructor and press Ctrl+A on keyboard and click on Enter to create constructor.
    • Now again placing cursor at next line of constructor closing brace , click ALT+INSERT and select getter and setter and again press CTRL+A to select all variables and hit Enter.

    That's it. Happy coding!!

    0 讨论(0)
提交回复
热议问题