Intellij (Android Studio)成员变量前缀

如何配置 Android Studio (通常是 Intellij)为带前缀的成员变量正确生成 getter 和 setter?

如果我有课的话

public class Foo{
int mBar;
}

当我做 Refactor->Encapsulate或者 Generate的时候,我希望它创建 getBar()而不是 getMBar

剪辑

我找到了每个项目的方法: 你只要走 File->Settings->Project Settings->Code Style

但它在我的项目目录的某个地方创建了一个设置文件。

我想要一个解决方案,让我做一次所有的项目,现在和未来。

22547 次浏览

Try going to File-->Other Settings-->Default Settings. Your should be able to make prefixes for every other project that you do if you change prefix settings with the scheme set to default instead of Project. The prefix change would be in Java and Code Generation. That will fix setters and getters.

In Mac OS

  • Android Studio -> Preferences -> Code Style -> Java
  • on the right panel, click on the right arrow until you see code generation tab
  • Click ok

enter image description here

In AS 2.2.1:

File > Settings > Editor > Code Style> Java > Code Generation > Naming > Name Prefix Field / Name Prefix Static Field

enter image description here

  1. Open Android Studio's preferences (from the Android Studio menu on Mac and from File -> Settings on Windows and Linux).
  2. Expand Editor and then expand Code Style.
  3. Select Java, then choose the Code Generation tab.
  4. In the Naming table, select the Field row and add m as the name prefix for fields. Then add s as the name prefix for static fields.

Code Generation Setting in Android Studio 3.x

  1. Click Ok

You need go to Setting > Editor > Code Style > Java and just specify s-prefix in static field, then click OK

enter image description here