android studio:如何使用fbc live模板 [英] android studio: how to use the fbc live template

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

问题描述

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.);

红色光标位于|处。您输入对象类型,可能使用选项卡或输入自动完成。您可能需要点击标签或在此之后再次输入以转到下一个字段:

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键选择Introduce Local Variable并将表达式赋值给一个新变量,从而发表声明:

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键即可完成。如果您想要一个不同的变量名称,请在输入之前开始键入新的变量名称。它们的变量名将自动替换,给出最终结果:

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 live模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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