Java Intellij (Android studio) 成员变量前缀

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22732722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:34:01  来源:igfitidea点击:

Intellij (Android studio) member variable prefix

javaintellij-ideacoding-styleandroid-studio

提问by Sergo Pasoevi

How do I configure Android Studio (or Intellij generally) to correctly generate getters and setters for member variables with prefixes?

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

If I have a class

如果我有一堂课

public class Foo{
    int mBar;
}

I want it to create getBar()instead of getMBarwhen I do Refactor->Encapsulateor Generate

我希望它创建getBar()而不是getMBar当我做Refactor->EncapsulateGenerate

EDIT

编辑

I found the way to do it per-project basis: You just go File->Settings->Project Settings->Code Style

我找到了按项目执行的方法:你去吧 File->Settings->Project Settings->Code Style

But it created a settings file somewhere in my project directory.

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

I want a solution which allows me to do it once for all projects, present and future.

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

采纳答案by Emil Re?a Enriquez

In Mac OS

在 Mac 操作系统中

  • Android Studio -> Preferences -> Code Style -> Java
  • on the right panel, click on the right arrow until you see code generation tab
  • Click ok
  • Android Studio -> 首选项 -> 代码风格 -> Java
  • 在右侧面板上,单击向右箭头,直到您看到 code generation tab
  • 单击确定

enter image description here

在此处输入图片说明

回答by Davis

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.

尝试转到文件--> 其他设置--> 默认设置。如果您更改前缀设置并将方案设置为默认而不是 Project,您应该能够为您所做的每个其他项目制作前缀。前缀更改将在 Java 和代码生成中进行。这将修复 setter 和 getter。

回答by Graeme

In AS 2.2.1:

在 AS 2.2.1 中:

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

文件 > 设置 > 编辑器 > 代码样式 > Java > 代码生成 > 命名 > 名称前缀字段/名称前缀静态字段

enter image description here

在此处输入图片说明

回答by Robert Odoch

  1. Open Android Studio's preferences (from the Android Studio menu on Mac and from File-> Settingson Windows and Linux).
  2. Expand Editorand then expand Code Style.
  3. Select Java, then choose the Code Generationtab.
  4. In the Namingtable, select the Fieldrow and add mas the name prefix for fields. Then add sas the name prefix for staticfields.
  1. 打开 Android Studio 的首选项(从 Mac 上的 Android Studio 菜单和Windows 和 Linux 上的文件->设置)。
  2. 展开编辑器,然后展开代码样式
  3. 选择Java,然后选择代码生成选项卡。
  4. 命名表中,选择字段行并添加m为 的名称前缀fields。然后添加s作为static字段的名称前缀。

Code Generation Setting in Android Studio 3.x

Android Studio 3.x 中的代码生成设置

  1. Click Ok
  1. 单击确定

回答by Dyno Cris

You need go to Setting> Editor> Code Style> Javaand just specify s-prefixin static field, then click OK

您需要转到Setting> Editor> Code Style> Java并在static field 中指定s-prefix,然后单击OK

enter image description here

在此处输入图片说明