gpt4 book ai didi

android - 自定义实时模板中的 groovy 脚本错误

转载 作者:行者123 更新时间:2023-11-29 01:10:20 25 4
gpt4 key购买 nike

我正在尝试创建一个自定义实时模板,以将 activity_main.xml 中的 View 作为全局变量绑定(bind)到 MainActivity.java(w/Butterknife),并将 View 的 ID 作为变量名称。

绑定(bind)脚本.groovy

//Arguments to be used in live template
def cN = _1 //'MainActivity'
def pN = _2 //'com.example.myapp'
def bool = _3 // true or false depending on whether we want the id or the view type


//Extract app Name from package. Assume that package name is name of project directory
def dN = pN.substring(pN.lastIndexOf(".") + 1).trim()

//Extract words from MainActivity camel-case. Assume layout file is activity_main.xml
layoutFileWords = cN.split("(?<!(^|[A-Z]))(?=[A-Z])|(?<!^)(?=[A-Z][a-z])")

//Make layout name corresponding to activity class
def layoutName = layoutFileWords[layoutFileWords.size()-1].toLowerCase()
for (int i = 0; i<layoutFileWords.size() - 1; i++) {
layoutName = layoutName + "_" + layoutFileWords[i].toLowerCase()
}
layoutName = layoutName + ".xml"

//Create layout directory path from package name
def layoutDir = "C:\\Users\\" + userName + "\\AndroidStudioProjects\\" + dN
+ "\\app\\src\\main\\res\\layout\\"

//Full layout name and qualified paths
def layoutFile = layoutDir + layoutName


//Read Layout File, parse XML and get Elements that have attribute "android:id"

// Assume we want to inflate all views with attribute "android:id"d
def String fileContents = new File(layoutFile).text

def layout = new XmlSlurper().parseText(fileContents)

def nodes = layout.depthFirst().findAll{
it["@android:id"].text().startsWith("@+id/")
}

def viewTypes = []
def ids = []
nodes.each { node ->
// println "Node Element: ${node.name().substring(node.name().lastIndexOf(".") + 1).trim()}"

// println "Node id: ${node["@android:id"].text().substring(5)}"
viewTypes.add(node.name().substring(node.name().lastIndexOf(".") + 1).trim())

ids.add(node["@android:id"].text().substring(5))
}

//return either View Type or id, depending on live template "bool" argument
if (bool) {
viewTypes[0]
} else {
ids[0]
}

这是实时模板

@Bind(R.id.$ID$)
$VIEWTYPE$ $ID$;

Name | Expression
ID | groovyScript("C:\Users\userName\bindScript.groovy", className(), currentPackage(), false)
VIEWTYPE | groovyScript("C:\Users\userName\bindScript.groovy", className(), currentPackage(), true)

这是 activity_main.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
android:id="@+id/editText1"
android:layout_width="85dp"
android:layout_height="43dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Players"
android:textAlignment="center"/>
</LinearLayout>

当我使用硬编码参数在 Android Studio 的 groovy 控制台中运行 groovy 脚本时,我返回了预期的结果。但是,当我尝试运行实时模板时,我在 MainActivity 中得到了这个

public class MainActivity extends AppCompatActivity {

@Bind(R.id.No such property:UserssnajerabindScript for class:Script1)
No such property:UsersuserNamebindScript for class:Script1
No such property:UsersuserNamebindScript for class:Script1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
}

我很确定我正确地传递了我的 groovy 脚本参数,但我必须承认我是 Live Templates 中的 groovyScripts 的新手,根据 docs因此非常感谢任何解决此错误的帮助。

最佳答案

我认为你需要转义反斜杠:

ID       |  groovyScript("C:\\Users\\userName\\bindScript.groovy", className(), currentPackage(), false)
VIEWTYPE | groovyScript("C:\\Users\\userName\\bindScript.groovy", className(), currentPackage(), true)

关于android - 自定义实时模板中的 groovy 脚本错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43647782/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com