我正在尝试创建一个自定义实时模板,将activity_main.xml中的视图绑定到MainActivity.java(w / Butterknife)作为全局变量,并将视图的id作为变量名称。
bindScript.groovy
CREATE OR REPLACE
FUNCTION CALCULATE_GPA
(p_studentID IN number)
RETURN NUMBER
IS
CURSOR cur_gpa IS
SELECT grade, grade_value, credit_hours
FROM grade
JOIN enrollment USING (Grade)
JOIN section USING (term_code, subject_code, course_number, section)
JOIN course USING (subject_code, course_number)
WHERE student_ID = p_studentID;
lv_gpa_calc NUMBER(4,2):=0;
BEGIN
FOR rec_gpa IN cur_gpa LOOP
lv_gpa_calc:= lv_gpa_calc + ((rec_gpa.grade_value * rec_gpa.credit_hours)/rec_gpa.credit_hours);
DBMS_OUTPUT.PUT_LINE(lv_gpa_calc);
END LOOP;
RETURN lv_gpa_calc;
END CALCULATE_GPA;
这是实时模板
//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]
}
这是activity_main.xml文件
@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)
当我在Android studio的groovy控制台中使用硬编码参数运行groovy脚本时,我会准确地返回预期的内容。但是,当我尝试运行实时模板时,我在MainActivity
中得到了这个<?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>
我很确定我正确地传递了我的groovy脚本参数,但是我必须承认我是docs中对于实时模板中的groovyScripts的新手,所以非常感谢任何解决此错误的帮助。
答案 0 :(得分:1)
我认为你需要逃避反斜杠:
ID | groovyScript("C:\\Users\\userName\\bindScript.groovy", className(), currentPackage(), false)
VIEWTYPE | groovyScript("C:\\Users\\userName\\bindScript.groovy", className(), currentPackage(), true)