Android Studio:如何使用 fbc 直播模板 [英] Android Studio: how to use the fbc live template

查看:34
本文介绍了Android Studio:如何使用 fbc 直播模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android Studio 支持宏——他们喜欢称之为实时模板——这很好,但没有人愿意解释如何使用它们,尤其是 Android Studio.很明显,fbc"实时模板旨在使 findViewById 更易于使用,但我不知道如何使用它.你究竟是如何使用这个模板来制作这样的样板文件的,例如:

Android Studio has support for macros—which they like to call live templates—which is nice, but nobody anywhere bothers to explain how to use them, least of all Android Studio. It is obvious that the "fbc" live template was designed to make findViewById easier to use, but I can't figure out how to use it. How exactly do you use this template to produce a piece of boilerplate like this, for instance:

EditText e = (EditText) findViewById(R.id.m);

推荐答案

经过一番折腾,我终于弄明白了.在空行上,输入fbc",然后点击 Tab.这让你明白:

After much fiddling, I did eventually figure it out. On a blank line, you type "fbc" then hit tab. That gets you this:

    (|) findViewById(R.id.);

红色光标位于|"处.您输入对象类型,可能使用 tab 或 enter 来自动完成.您可能需要点击 Tab 键或在此之后再次输入才能进入下一个字段:

with a red cursor placed at the "|". You enter the object type, possibly using tab or enter to autocomplete. You might need to hit tab or enter again after that to move onto the next field:

    (EditText) findViewById(R.id.|);

对 ID 重复.这让你明白:

Repeat for the ID. That gets you this:

    (EditText) findViewById(R.id.m)|;

整行都带有下划线,因为它是一个表达式而不是一个语句.但是,任何时候只要有一行带有表达式的行,就可以按 Alt-Enter,然后再次 Enter 以选择引入局部变量"并将表达式分配给一个新变量,从而做出声明:

The whole line will be underlined because it is an expression and not a statement. Any time you have a line with an expression on it by itself, though, you can hit Alt-Enter, then Enter again to select "Introduce Local Variable" and assign the expression to a new variable, thus making a statement:

    EditText |viewById| = (EditText) findViewById(R.id.m);

它会自动生成一个新的变量名.如果你没问题,只需按 Enter 即可完成.如果您想要不同的变量名称,请在按 Enter 之前开始输入新的变量名称.他们的变量名会被自动替换,给出最终结果:

It generates a new variable name automatically. If you're fine with it, just hit enter to finalise. If you want a different variable name, start typing the new variable name before hitting enter. Their variable name will automatically be replaced, giving the final result:

    EditText e = (EditText) findViewById(R.id.m);|

而且就是您使用fbc"实时模板的方式!IMO,这应该是每个 Android 教程的一部分.

And that's how you use the "fbc" live template! IMO, this should be a part of every Android tutorial.

我后来意识到 fbc 模板制作得很差,修复它要容易得多,进入设置并用这个替换它的模板文本:

I later realised the fbc template was poorly made and it's far easier just to fix it, going into the settings and replacing its template text with this:

$cast$ $var$ = ($cast$) findViewById(R.id.$resId$);

这一次就完成了所有事情.在填充值之前它看起来有点奇怪.

That does the whole thing all at once. It just looks a little weird until after the values are filled in.

这篇关于Android Studio:如何使用 fbc 直播模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆