简体   繁体   English

Intellij Idea Live模板可同时创建字段和方法

[英]Intellij Idea Live Template to create field and method at same time

How to create field variable automatically when I create method used that field. 当我创建使用该字段的方法时,如何自动创建字段变量。 I've create template like this: 我已经创建了这样的模板:

void $METHOD_NAME$() {
    $FIELD_NAME$ = true;
}

when I type field name (eg mState ) in method will create field as: 当我在方法中键入字段名称(例如mState )时,将创建字段为:

private boolean mState = false;

Hope someone help. 希望有人帮忙。 Sorry my bad. 对不起这是我的错。

Given the screenshot of your template, you can also create a field with the following live template: 给定模板的屏幕快照,您还可以使用以下实时模板创建一个字段:

private boolean $param$ = false;

@Override
public void onBackPressed() {
  if ($param$) super.onBackPressed();
  android.widget.Toast.makeText(this, "$message$",
    android.widget.Toast.LENGTH_SHORT).show();
  $param$ = true;
  final Handler handler = new Handler();
  handler.postDelayed(new Runnable() {
    @Override
    public void run() {
      $param$ = false;
    }
  }, 100);
}

Where $param$ and $message$ are regular variables without anything special. 其中$ param $和$ message $是常规变量,没有任何特殊之处。

However, like I said in the comment on your question, I suggest to split it up in several smaller templates. 但是,就像我在对您的问题的评论中所说的那样,我建议将其分成几个较小的模板。 Consider to split it up in: field + method with just: 考虑将其拆分为:field + method仅带有:

private boolean $param$ = false;    

@Override
public void onBackPressed() {
  if ($param$) super.onBackPressed();
  $param$ = true;
}

Then create a template for the message: 然后为消息创建一个模板:

android.widget.Toast.makeText(this, "$message$", android.widget.Toast.LENGTH_SHORT).show();

And last but not least, create a template for the postDelayed: 最后但并非最不重要的一点是,为postDelayed创建一个模板:

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        $END$
    }
}, $delay$);

Note: the $delay$ as a bonus you can even give it a default value or create a list of predefined values for ease of use. 注意: $delay$作为奖励,您甚至可以给它一个默认值或创建一个预定义值列表,以方便使用。

Note2: Instead of $param$ = false; 注意2:代替$param$ = false; I've replaced it with $END$ . 我已将其替换为$END$ This will position your cursor here once you've selected the delay. 选择延迟后,这会将光标定位在此处。 Now you can type mState = false manually here, or whatever code you need in the context at that moment. 现在,您可以在此处手动键入mState = false,或此时需要在上下文中输入的任何代码。 This makes the template much more flexible and easier to use. 这使模板更加灵活且易于使用。

PS. PS。 I suppose you want to call super.onBackPressed() only when the value is false (on the first invocation). 我想您只想在值是false时(在第一次调用时)调用super.onBackPressed() )。 In that case use if (!$param$) instead. 在这种情况下,请使用if (!$param$)

// Update: //更新:

In order to group the newly added field with the other fields and not halfway somewhere in your class between other methods, rearrange the code via the menu with: Code -> rearrange code . 为了将新添加的字段与其他字段组合在一起,而不是在类中其他方法之间的中间位置,请通过菜单使用Code -> rearrange code To customise this, check your arrangement settings under: settings -> code style -> <language> -> arrangement 要对此进行自定义,请在以下位置检查您的排列设置: settings -> code style -> <language> -> arrangement

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM