Live Template btnClick in Android Studio 2.1

1

I am getting used to using the live templates that Android Studio has. ctrl+j : the available ones are shown.

I try to create a Live Template that starts, writing btnClick , the purpose is to complete the assignment code of a click event to a button.

Live template code in xml:

<templateSet group="Android">
  <template name="btnClick" value="Button $var0$ = (Button) findViewById(R.id.$var1$);&#10;if ($var0$ != null) {&#10;    $var0$.setOnClickListener(new View.OnClickListener() {&#10;&#10;        @Override&#10;        public void onClick(View v) {&#10;            //TODO click Event&#10;            $END$&#10;        }&#10;    });&#10;}" description="Generate Click Button Event" toReformat="false" toShortenFQNames="true">
    <variable name="var0" expression="" defaultValue="" alwaysStopAt="true" />
    <variable name="var1" expression="complete()" defaultValue="" alwaysStopAt="true" />
    <context>
      <option name="JAVA_CODE" value="false" />
      <option name="JAVA_STATEMENT" value="true" />
      <option name="JAVA_EXPRESSION" value="false" />
      <option name="JAVA_DECLARATION" value="false" />
      <option name="JAVA_COMMENT" value="false" />
      <option name="JAVA_STRING" value="false" />
      <option name="COMPLETION" value="false" />
    </context>
  </template>
</templateSet>

command: btnClick

code:

Button $var0$ = (Button) findViewById(R.id.$var1$);
if ($var0$ != null) {
    $var0$.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            //TODO click Event
            $END$
        }
    });
}

Variable declaration

$var0$ nothing * I try to show by default myButton1, if the user does not change anything

$var1% = > Complete action, so that a list of the ids of the layout is displayed.

Applicable in : Java: Statement

Not if it is possible to define a default value in $ var0 $ with the text myBtn1 in case it is not modified.

    
asked by Webserveis 03.05.2016 в 13:36
source

1 answer

0

Trying to put the text myBtn1 by default in the attribute defaultValue of the var0, without success, I put to look for documentation of the live tempaltes.

  • How to create live templates (en)
  • Actions on variables
  • The solution: To specify literals you must use "

    The definition of $var0$ in the live template

    ... defaultValue="&quot;my_Button1&quot;" ...
    

    final template:

    <templateSet group="Android">
      <template name="btnClick" value="Button $var0$ = (Button) findViewById(R.id.$var1$);&#10;if ($var0$ != null) {&#10;    $var0$.setOnClickListener(new View.OnClickListener() {&#10;&#10;        @Override&#10;        public void onClick(View v) {&#10;            //TODO click Event&#10;            $END$&#10;        }&#10;    });&#10;}" description="Generate Click Button Event" toReformat="false" toShortenFQNames="true">
        <variable name="var0" expression="" defaultValue="&quot;my_Button1&quot;" alwaysStopAt="true" />
        <variable name="var1" expression="complete()" defaultValue="" alwaysStopAt="true" />
        <context>
          <option name="JAVA_CODE" value="false" />
          <option name="JAVA_STATEMENT" value="true" />
          <option name="JAVA_EXPRESSION" value="false" />
          <option name="JAVA_DECLARATION" value="false" />
          <option name="JAVA_COMMENT" value="false" />
          <option name="JAVA_STRING" value="false" />
          <option name="COMPLETION" value="false" />
        </context>
      </template>
    </templateSet>
    
        
    answered by 03.05.2016 / 14:49
    source