美文网首页
as设置模板代码(kotlin)

as设置模板代码(kotlin)

作者: 王家匀匀 | 来源:发表于2020-12-08 11:14 被阅读0次

我比较常用的功能:创建一个activity,并且传参。
先给步骤图:


步骤图.png

以下这句可以获取到当前activity。

groovyScript("_1 ?: '<top>'", kotlinClassName())
变量设置位置.png 设置表达式.png
 companion object {
 
        const val $key$ = "$key$"

        fun newActivity(activity: android.app.Activity,$args$:String): android.content.Intent {
            val intent = android.content.Intent(activity, $class$::class.java)
            intent.putExtra($key$,$args$)
            return intent
            
        }
    }

同时,变量经常还需要保存起来。
所以有了进化版本:(左边特意空格,适应格式化代码)

    private var $args$: String = ""
    
    companion object {
        val TAG = $class$::class.java.name
        const val $key$ = "$key$"
    
        fun newActivity(activity: android.app.Activity,$args$:String): android.content.Intent {
            val intent = android.content.Intent(activity, $class$::class.java)
            intent.putExtra($key$,$args$)
            return intent
        }
    }
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    
        $args$ = intent?.getStringExtra($key$)
                ?: savedInstanceState?.getString($key$) ?: ""
    }
    
    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        outState.putString($key$, $args$)
    }

步骤是:
as->help-find action->Live templates->add templates group->add templates(in your group)->copy script ->edit variables-> set value Expression-> Define -> apply.
要注意设置适用范围 Define Kotlin. 这样才能起效。
魔板代码apply后,立即生效。无需重启as.

设置步骤,参考:https://blog.csdn.net/wubihang/article/details/51228752

相关文章

网友评论

      本文标题:as设置模板代码(kotlin)

      本文链接:https://www.haomeiwen.com/subject/pkyfgktx.html