Chapter 2 Navigating Android Studio(下)

The TODO Tool Window

待办事项工具窗口

TODO means, of course, to do. TODOs are essentially comments that indicate to the programmers and their collaborators that there remains work yet to be done. TODOs are written like comments, beginning with two forward slashes, the word TODO in all-caps, and a space. For example:

TODO的意思当然就是to do。TODO对程序员来说是非常重要的,说明他们的合作伙伴留下的还没做完的工作。TODO写出来就像注释一样,开始是两个斜杠,还有单词TODO,再加一个空格。例如下面这样:

//TODO inflate the layout here.

Create a TODO in MainActivity.java and open the TODO tool window to view it. Clicking a TODO in the TODO tool window immediately jumps to that TODO in your source code.

在MainActivity.java文件中创建一个TODO,打开TODO工具窗口查看。在TODO工具窗口中点击TODO,立刻在你的代码资源中跳出来一个TODO。

The Commander Tool Window

Commander工具窗口

The Commander tool window is a navigation aid with left and right panes. These panes function much like the Project and Structure tool windows do. The Commander tool window differs from these other navigation windows in that it displays only one directory level at a time, rather than displaying nested directory trees. If you prefer Windows-style navigation or you find that the Project tool window is too overwhelming, then the Commander tool window may be a good navigation alternative.

Commander工具窗口是一个航标,位于面板的左右两侧。这些面板功能非常像Project和Structure工具窗口。Commander工具窗口不同于其它的导航窗口一次只能显示一个目录级,它可以显示目录树。如果你更喜欢窗口风格的操作,或者你发现工程工具窗口显示内容太多了,Commander工具窗口可能是一个好的操作选择。

The Main Menu Bar

主菜单工具栏

The main menu bar is the uppermost bar in Android Studio, and you can execute virtually any action by navigating through its menus and submenus. Unlike the other bars in Android Studio, the main menu bar cannot be hidden. Don’t be overwhelmed by the many actions contained in the main menu bar and its submenus. Even the most seasoned Android developer will use only a fraction of these actions on a daily basis, and most of the actions have corresponding keyboard shortcuts and/or context menu items. We discuss many of the actions contained in the main menu bar in subsequent sections and chapters.

在Android S Studio中,主菜单工具栏是最重要的工具栏。你可以实际操作它的菜单和子菜单。不像在Android Studio中的其它菜单栏,主菜单栏是不能被隐藏的。不要被主菜单和子菜单包含的很多操作给压倒。设置经验丰富的Android开发者每天都会使用这些功能,大多数的操作都有相应的快捷键或者是快捷菜单。在之后的部分和章节中我们会讨论很多包括主菜单栏在内的很多操作。

The Toolbar

工具栏

The toolbar contains buttons for frequently used text operations such as Cut, Copy, Paste, Undo and Redo. As you’ve already seen in Chapter 1, the toolbar also contains buttons to various managers within Android Studio, including the SDK Manager and the Android Virtual Device Manager. The toolbar also has buttons for Settings and Help, as well as buttons to Run and Debug your app. All of the buttons in the toolbar have corresponding menu items and keyboard shortcuts. Advanced users may want to hide the toolbar to conserve screen real-estate by unchecking the View ➤ Toolbar menu item.

工具栏包含很多比较常用的按钮,比如cut(剪切)、copy(复制)、Paste(粘贴)、undo(撤销)、redo(恢复)。就像你已经在第一章中看到的一样,工具按钮也包含Android Studio中的很多的管理器,包括SDK管理器和Android虚拟设备管理器。工具栏也有setting(设置)和Help(帮助)按钮,还有两个按钮是用来控制你的app的:Run(运行)、Debug(调试)。所有在工具栏中按钮都有相应的菜单项和快捷键。高级用户可能想要隐藏工具栏来保证实际屏幕的使用面积足够大。这个操作通过取消View->Toolbar菜单项。

The Navigation Bar

导航栏

The navigation bar displays a horizontal chain of arrow boxes representing the path from your project’s root directory (on the left) to the currently selected tab in the Editor (on the right). The navigation bar may be used to navigate your project’s assets without having to resort to the Project or Commander tool windows.

导航栏是一个水平链条,显示了你的工程的根目录(在左边)到你当前被选中的页面(在右边)的路径。导航栏不需要依靠Project tool windows或者commander tool windows就可以被用来导航工程信息

The Status Bar

状态栏

The status bar, shown in Figure 2-5 (and previously in Figure 2-1), displays relevant and context-sensitive feedback, such as information about any running processes or the state of your project’s Git repository. Let’s explore the status bar in some detail now.

如图2-5所示,就是状态栏,显示了相关的和上下文敏感的反馈,比如运行过程的信息或者是你的工程的git库的状态。让我们现在了解下状态栏的一些细节。


In the leftmost corner of the status bar is the Toggle Margins button. Clicking this button toggles hiding and showing the margins. In addition, when you hover your mouse over this button, a context menu appears that allows you to activate any of the tool windows. 

在状态栏最左边角落里的是Toggle Margin按钮。点击这个按钮就会显示或者隐藏编辑器的边界。除此之外,把你的鼠标放在这按钮周围,一个快捷菜单就会出现,允许你激活任何工具窗口。

The message area is used to provide feedback and display any information about concurrently running processes. This area also displays hints as you roll your mouse over UI elements such as menu items or buttons in the toolbar. Clicking on this area opens the Event log. The Editor cursor position displays the location of your cursor in the Editor in line:column format. Clicking on this area activates a dialog box allowing you to navigate directly to a particular line in your code.

信息域可以提供反馈和显示一些关于正在运行的进程的信息。当你在UI元素周围滚动你的鼠标的时候,这个区域也显示一些提示,比如工具栏中的菜单项。点击这片区域会打开Event log(事件日志)。编辑器光标的位置显示了你的光标在编辑器中行和列的位置。点击这片区域打开一个对话框,这个对话框允许你直接导航到你的代码的具体某一行。

The line separator area displays the format of the carriage returns used in your text files. On Windows, the default is CRLF, which stands for carriage return line feed. LF is the standard format used on Unix and Mac machines, as well as in Git. If you’re developing on a Windows computer, Git will typically convert from CRLF to LF when committing your code to the repository.

行分隔符显示了在你的文本文件中回车的使用方式。在windows中,默认就是回车换行符,这代表了回车换行。LF(换行)是使用在Unix和Mac还有Git中的标准形式。如果你在windows环境中开发,当你要提交你的代码到代码库的时候,Git通常是将回车换行转换成换行。

The text format area describes the text encoding used for your source files. The default is UTF-8, which is a superset of ASCII and includes most of the Western alphabets, including any characters that you might find in a standard Java or XML file.

文本格式域描述了你的文件的文本编码方式。默认的编码方式是UTF-8。UTF-8是ASCII码的一个超集,它包含了大多数的西方的文字,这其中含有一些你可能在标准Java或者XML发现的字母。

The file access indicator area allows you to toggle between read/write and read-only. An unlocked icon means that the current file in the Editor has read/write access. A lock icon means that the current file in the Editor is read-only. You can toggle these settings by clicking the indicator’s icon.

文件访问指示器允许你在读/写和只读之间进行切换。一个开锁的标志意味着编辑器中的当前文件有读/写权限。一个锁的标志的意思是当卡你的文件只有读的权限。你可以通过点击指示器的图标来进行自由设置。

The Highlighting Level button activates a dialog box with a slider that allows you to set the level of highlighting you want to see in your code. The default setting is Inspections, which corresponds to an icon of a frowning Inspections Manager. This setting indicates that you should be prepared for some tough love, as the Inspections Manager will be strict in identifying both syntax errors and possible problems with your code, called warnings. You can see some of the warnings generated by the Inspections Manager in the marker bar as yellow ticks.

高亮水平按钮激活了一个带着滑动条的对话框,这回允许你设置你的代码的语法高亮的水平。默认的设置是Inspection(检查),对应的是一个褶皱的图标,意思是Inspections Manager(检查管理器)。这个设置表预示着你应该会很喜欢这个,因为Inspection Manager将会对你的代码中可能出现的问题和语法错误进行非常严格的检查,我们叫它作警告。你可以在标记栏中看到由Inspection Manager收集起来的黄色标识的警告信息。

The next setting on the slider is Syntax, which corresponds to an icon of the Inspections Manager in profile. For this setting, the Inspections Manager is turning a blind eye to warnings. Syntax mode is less strict than Inspections mode, but still highlights problems with syntax that will prevent your code from compiling.

在滑动器中的下一个设置是Syntax(语法),对应着属性中的Inspection Manager的一个图标。有了这个设置,Inspection Manager就会无视警告信息。Syntax模式并没有Inspection模式严谨,但是仍然会对阻碍你代码成功编译的语法问题进行高亮提示。

The last highlight mode on the slider is None, which corresponds to an icon of a smiling Inspections Manager. This icon makes me think that the Inspections Manager is happy-drunk and just doesn’t care about your code. Even the most egregious syntax errors are ignored in this mode, though the compiler will still choke on them when you attempt to build. I recommend leaving the highlight level to Inspections and learning to appreciate the Inspections Manager’s tough love.

在滑动器中的最新的高亮模式是None(空),对应着一个微笑的Inspection Manager图标。这个图标会让我们联想到Inspection Manager在快乐的饮酒,根本不关心的代码问题。在这种模式下,甚至最可恶的语法错误都会被忽略。尽管当你试图创建它们的时候将会导致编译错误。我建议设置为Inspection,然后试着去喜欢上Inspection Manager。

Common Operations

常见的操作

This section reviews various common operations used in Android Studio. If you’ve used a text editor like Microsoft Word, you will likely be familiar with the features covered in this section.

这一部分会带你复习在Android Studio中的常见操作。如果你使用类似于Word的文本编辑器,你将会对这部分的内容非常熟悉。

Selecting Text

选择文本

As you would expect from any good text editor, double-clicking any word in a source file selects it. In addition, clicking and dragging the cursor across letters or words selects those text elements. Placing your cursor anywhere in a source file and pressing Shift+Down-Arrow or Shift+Up-Arrow selects lines of text beginning at the cursor. Triple-clicking anywhere on a line of text selects the entire line. Pressing Ctrl+A | Cmd+A selects all text in a file.

就像你期望的一款好用的文本编辑器一样,双击资源文件中的任何单词就是选中了这整个单词。除此之外,单击并拖动你已经选中的文本。将你的光标放置在文件中的任意位置,按住shiift+向下箭头或者shift+向上箭头来选择文本在光标中的开始行。在文件的某一行三击鼠标就会选中这一行。按ctrl+A|cmd+A全选文本内容。

If you place your cursor inside any word and press Ctrl+W | Alt+Up-Arrow, the entire word becomes selected. If you continue to press Ctrl+W | Alt+Up-Arrow, the selection grows to include adjacent text ad infinitum. If you now press Ctrl+Shift+W | Alt+Down-Arrow, the selection shrinks. This growing/shrinking selection functionality is called structural selection in Android Studio.

如果你将你的光标放置在任意一个单词的中间然后按ctrl+W|alt+向上箭头,这整个单词就被选中了。如果你继续按ctrl+W|alt+向上箭头,选中的内容就会增长到临近的文本,并且是无限增长的。如果你按ctrl+shift+W|alt+向下的箭头选中就会被收起。这个伸展/收缩的选择功能在Android Studio中就被叫做Structural selection。

Using Undo and Redo

使用撤销和恢复

The Undo and Redo commands are useful for rolling back and rolling forward a limited number of edit operations. Changes are delimited by specific UI events such as pressing Enter or repositioning the cursor. The keyboard shortcuts for Undo and Redo are Ctrl+Z | Cmd+Z and Ctrl+Shift+Z | Cmd+Shift+Z, respectively. There are purple right- and leftarrows on the left side of the toolbar that will do the same. The default on Android Studio is to remember all your steps back to your last save or up to 300 steps. Undo and Redo are applied to only one file at a time, so the most effective way to roll back changes is to use Git, which is discussed in Chapter 7.

撤销和恢复操作对于在编辑操作数量受限的情况下进行回滚和前滚是非常有用的。变化是被特定的UI事件分隔开了,比如按回车或者移动光标。撤销和恢复的快捷键分别是ctrl+z|cmd+z和ctrl+shift+z|cmd+shift+z。在工具栏左侧的紫色向右或者向左箭头也可以完成同样的操作。在Android Studio中默认的是记住你直到你最近保存的所有的操作或者最高记住300步操作。撤销和恢复在同一时刻只能应用于一个文件,因此最有效的回滚方式就是使用Git,这个我们会在第七章讨论。

Finding Recent Files

寻找最近使用的文件

Among the best features of Android Studio is that it remembers all the files you worked on recently. To activate this command, choose View ➤ Recent Files or press Ctrl+E | Cmd+E. The resulting dialog box allows you to select any recent file and opens it as a tab in the Editor. The default limit remembers up to 50 previous files. You can change these limits by navigating to File ➤ Settings ➤ Limits ➤ Editor ➤ Recent Files Limit.

Android Studio中所有的最好的特点就是记住你最近使用的所有文件。为了激活这个功能,选择View->Recent Files或者按Ctrl + E|Cmd+E。弹出来的结果对话框允许你选择任何最近的文件,你可以在编辑器中打开它。默认的能记忆的文件个数的上限是50个。你可以通过下面的操作设置这个上限:File->Settings->Limits->Editor->Recent Files Limit。 

Traversing Recent Navigation Operations

遍历最近的导航操作

Android Studio also remembers your recent navigation operations. Navigation operations include cursor moves, tab changes, and file activations. To traverse your navigation operations history, press Ctrl+Alt+Left-Arrow | Cmd+Alt+Left-Arrow or Ctrl+Alt+Right-Arrow | Cmd+Alt+Right-Arrow. Keep in mind that navigation operations are different from edit operations; if you want to traverse your edit operations, you should use Undo and Redo.

Android Studio也记住了你最近的导航操作。导航操作包括你的鼠标移动,标签变化,文件激活。为了遍历你的导航操作的历史,Ctrl+Alt+左箭头|Cmd+Alt+左箭头或者Ctrl+Alt+右箭头|Cmd+Alt+右箭头。记住导航操作跟编辑操作不同。如果你想要遍历你的编辑操作,你应该使用撤销和恢复

Cutting, Copying, and Pasting

剪切、复制和粘贴

If you’ve used any text editor or word processor, you’re familiar with Cut, Copy, and Paste. Table 2-2 lists these basic commands, as well as some of the extended clipboard commands.

如果你用过一些文本编辑器或者文字处理器,你对这三个功能是十分熟悉的。表2-2列出了基本的命令,还有一些扩展的剪切板命令。

Table 2-2. Cut, Copy, and Paste


Command                                 PC Keys                                 Mac Keys

Cut                                            Ctrl+X                                      Cmd+X

Copy                                         Ctrl+C                                      Cmd+C

Paste                                         Ctrl+V                                      Cmd+V

Extended Paste                     Ctrl+Shift+V                          Cmd+Shift+V

Copy Path                               Ctrl+Shift+C                          Cmd+Shift+C

Copy Reference                   Ctrl+Alt+Shift+C                    Cmd+Alt+Shift+C


In addition to the simple Cut, Copy, and Paste functionality provided by the OS clipboard, Android Studio has an extended clipboard that remembers the last five Cut and Copy operations. When you cut or copy text from Android Studio—or virtually any other application while Android Studio is running—Android Studio places that text onto a stack. To see the extended clipboard stack, press Ctrl+Shift+V | Cmd+Shift+V. The resulting dialog box allows you to choose whichever item you’d like to paste. See Figure 2-6.

除了操作系统提供的剪切、复制和粘贴功能,Android Studio也有扩展的剪切板功能,一共能记住最近五次的剪切和复制操作。当你从Android Studio中剪切或者复制文本时------或者实际上当Android Studio运行的时候其它的应用——Android Studio会将文本放进一个堆。为了看到这个扩展的剪切板的堆,按Ctrl+Shift+V|Cmd+Shift+V。结果对话框允许你选择任何你想要粘贴的内容。如图2-6:

Figure 2-6. Extended clipboard


You can also change the size of the extended clipboard stack by navigating to File ➤ Settings ➤ Limits ➤ Editor ➤ Maximum Number of Contents to Keep in Clipboard. You can also compare any currently selected text with that of the most recent element in the extended clipboard by right-clicking the selection and selecting the Compare with Clipboard menu item.

你也可以自己设置扩展的剪切板堆的大小:File->Setting->Limits->Editor->Maximum Number of Contents to Keep in Clipboard。你也可以在剪切板菜单项中选择Compare来将当前被选中的文本和扩展剪切板中内容进行对比,这个操作通过右击选中内容完成。

The Copy Path command Ctrl+Shift+C | Cmd+Shift+C copies the fully qualified operating system path of any file or directory selected in the Project or Commander tool windows, or any tab of the Editor. Copy Path is particularly useful for operations in a terminal session.

复制路径命令Ctrl+Shift+C|Cmd+Shift+C复制了在工程或者命令工具窗口或编辑器某一页中被选中的任何文件或目录的绝对路径。Copy Path操作在终端中的操作尤其有用。

With Copy Reference Ctrl+Alt+Shift+C | Cmd+Alt+Shift+C, Android Studio allows you to copy a logical reference to a method, variable, or class. When you paste this reference into another source file, Android Studio automatically includes any required package qualifiers and imports. You can also use generic Cut, Copy, and Paste on packages, directories, and files in the Project and Commander tool windows in lieu of mouse operations such as drag-and-drop in order to reorganize the location of assets in your project.

使用复制索引操作Ctrl+Alt+Shift+C|Cmd+Alt+Shift+C,Android Studio允许你复制一个逻辑索引到一个方法,变量,或者一个类。当你复制这个索引到另外一个文件的时候,Android Studio会自动导入所需要的包和入口。你也可以使用通用的剪切、复制和粘贴在工程和Commander工具窗口的包、目录和文件上,这是为了代替鼠标操作,比如通过拖放操作来重新组织你的工程中的文件

Context Menus

快捷菜单

Numerous context menus can be activated by right-clicking (Ctrl-clicking on Mac) on the IDE. You’ve already explored the Editor tab context menu in a previous section. Most panes, icons, and bars in Android Studio will generate a context menu if you right-click (Ctrl-click on Mac) it. One of the greatest features of Android Studio is that actions may be performed in more than one way. This redundancy means that you are free to develop your skills and habits according to your own preferences. I find that using keyboard shortcuts for the most frequent operations, and menu and context-menu actions for less-frequent operations is the most effective way to interface with Android Studio. Explore the context menus by rightclicking (Ctrl-clicking on Mac) bars, tabs, panes, and files in the IDE now.

在IDE中大量的快捷菜单都可以通过右击(Mac中是Ctrl)来激活。你已经在前面的部分找到了编辑器中的页面快捷菜单。在Android Studio中的大多数的面板,视图还有标题栏都可以通过右击鼠标弹出来快捷菜单。Android Studio中最大的一个特点就是一个操作可以通过不仅一种方式进行。这种冗余意味着你可以按照你的喜好自由的养成你的使用习惯。我发现在Android Studio中是用快捷键对于一些频繁的操作和对于较少使用的菜单、快捷菜单的使用时最有效率的。现在在你的IDE通过右击标题栏、面板、标签和文件找到相应的快捷菜单。

Getting Help

取得帮助

The Help menu in Android Studio has several useful menu items. Find Action (Ctrl+Shift+A | Cmd+Shift+A) is the command you will use most often to get help in Android Studio. This command activates a dialog box that allows you to search for any feature in Android Studio. Press Ctrl+Shift+A | Cmd+Shift+A and type Show Line Numbers in the search box. Now use your arrow keys to select Settings and press Enter. In the Settings window, choose Editor ➤ Appearance. You should see the Show Line Numbers check box.

在Android Studio中的帮助菜单中有很多有用的菜单项。Find action(发现操作)(Ctrl+Shift+A|Cmd+Shift+A)在Android Studio中是最常使用的找到帮助的方式。这个命令激活了一个对话框,在这个对话框里你能找到一些关于Android Studio的特性。按Ctrl+Shift+A|Cmd+Shift+A然后再查找对话框里键入Show Line Number(显示行号)。现在使用你的方向箭头来选择设置然后回车。在设置窗口中,选择Editor->Appearance。你应该看到显示行号的对话框。

Choosing Help ➤ Online Documentation is your source to all the technical specifications for Android Studio. This is the most comprehensive documentation for Android Studio. Also, the Help ➤ Default Keymap Reference menu item is a useful reference. You may consider printing this PDF and keeping it nearby as you learn to use Android Studio.

选择Help->Online Documentation是AndroidStudio的所有技术规格的资源。这是Android Studio综合性最强的文档。Help->Default Keymap Reference菜单项也是一个很有用的索引。你可以打印这个PDF,当你学习PDF的时候放在你的周围。

Navigating with the Keyboard 

快捷键操作

The keyboard is perhaps the most powerful way to navigate around Android Studio. Select the Navigate menu from the main menu bar to inspect its contents. This section discusses the most important menu items (shown in Table 2-3) and their corresponding keyboard shortcuts from the Navigate menu. Subsequent chapters discuss other menu items.

快捷键操作可能是操作Android Studio最高效的方式。从主菜单中选择导航菜单来查看它的内容。这一部分讨论了最重要的菜单项(如表2-3所示)和来自导航菜单的相对应的快捷键。下一章将会讨论其它的菜单项

Table 2-3. Keyboard Navigation


Command                                     PC Keys                                          Mac Keys

Select In                                       Alt+F1                                                  Alt+F1

Class                                             Ctrl+N                                                   Cmd+O

File                                               Ctrl+Shift+N                                        Cmd+Shift+O

Line                                               Ctrl+G                                                      Cmd+L

Related File                                  Ctrl+Alt+Home                                      Alt+Cmd+Up-Arrow

Last Edit Location                       Ctrl+Shift+Backspace                           Cmd+Shift+Backspace

Type Hierarchy                              Ctrl+H                                                     Ctrl+H

Declaration                                     Ctrl+B                                                        Cmd+B


Select In

选择性进入

One of the best features of Android Studio is that navigation is bilateral. You’ve already seen how to open/activate files as tabs of the Editor from various tool windows. Now you’re going to learn how to navigate to various tool windows from the Editor.

Android Studio最重要的一个特点是导航是双边的。你已经了解了怎行从各种各样的工具窗口中打开文件作为编辑器的页面。现在你要学习如何如何从编辑器的各种各样的工具窗口中进行操作。

Press Alt+F1. This activates the Select In context menu, which contains several menu items, including Project View, Favorites, and File Structure. Click the Project View option. The Project tool window becomes activated, the file corresponding to the active tab of the Editor is highlighted, and any parent directories of that file are toggled open. Android projects tend to have a lot of file assets; therefore, using Select In is among the most important skills that you will master.

按下Alt+F1。这个打开Select In的快捷菜单。它会包含很多的菜单项,包括工程视图,收藏夹,还有文件结构。点击Project View(工程视图)选项。工程工具窗口就被打开了,相应的文件编辑器的页面就会亮起来。一些文件的父目录也会被打开。Android工程有很多的文件;因此,使用Select In是你要掌握的最重要的技能。

Class

The Class action allows you to navigate to a particular Java class. It’s important to note that this action searches for only Java source files, or inner classes of Java source files. Press Ctrl+N | Cmd+O and start typing act. Android Studio has already indexed all of your files, and so it will provide you a list of possible matches, with the most likely match highlighted. All you need to do is press Enter to open MainActivity.java.

Class(类)操作允许你操作Java中的某个类。你需要记住的是这个操作只查找Java文件或者Java内部类的文件。按Ctrl+N|Cmd+O然后开始键入act(操作)。Android Studio已经索引了你的所有文件,因此将会提供给你一个可能匹配的清单,最有可能匹配的将会有高亮显示。你所要做的就是按回车打开MainActivity.java。

File

文件

The File action allows you to navigate to any file in your project. If you’re looking for an XML file in your project, this is the action that you will want to use. Press Ctrl+Shift+N | Cmd+Shift+O and start typing act. We’ve used the same search term act on purpose to illustrate the wider scope of Navigate ➤ File. Notice that the search results include the Java source file MainActivity.java as well as any other files, such as activity_main.xml. Use the arrow keys to select activity_main.xml and press Enter to open it.

文件操作允许你找到你工程中的任何文件。如果你正在你的工程中查找XML文件,这个操作可能就是你想要的。按Ctrl+Shift+N|Cmd+Shift+O然后开始键入操作(act)。我们使用同一个查找单词act就是为了说明Navigate->File的范围是比较宽泛的。注意到查找结果包括MainActivity.java还有其它的文件,比如activity_main.xml文件。使用方向箭头来选择activity_main.xml然后按回车打开它。

Line

The Line action Ctrl+G | Cmd+L activates a dialog box that allows you to navigate to a particular line:column of your source file. If you type a simple integer in the resulting Go to Line dialog box and press OK, Android Studio will jump to that line without regard to column.

行操作Ctrl+G|Cmd+L激活了一个对话框,在这个对话框中可以允许你操作你的文件的某一行。如果你在对话框Go to Line中键入一个简单的整数,然后按OK,Android Studio将会不考虑列调到那一行。

Related File

相关文件

The Related File action Ctrl+Alt+Home | Alt+Cmd+Up-Arrow is one of the most useful commands in Android Studio. Android projects typically have a lot of related files. For example, a simple Android activity usually has at least one corresponding XML layout file that renders the activity’s layout, and one corresponding XML menu file that renders the activity’s menu. As you work with fragments, this complexity only increases. You’ve already seen how to group related files together by using Favorites. With Navigate ➤ Related File, you can query Android Studio to show you related files. With the MainActivity.java tab activated, press Ctrl+Alt+Home | Alt+Cmd+Up-Arrow. You should see activity_main.xml listed there. Use your arrow keys to select it and press Enter.

相关文件操作Ctrl+Alt+Home|Cmd+Alt+上箭头是Android Studio中最有用的命令。Android工程通常会有很多的相关文件。例如,一个Android的activity通常至少有一个相关的XML布局文件来渲染activity的布局,还有一个相关的XML菜单来渲染activity的菜单。就像你操作fragment,这个复杂性就会增加。你已经知道了怎样使用Favorites来关联整个组的文件。使用操作Navigate->Related File,你可以询问Android Studio显示相关联的文件。MainActivity.java标签被激活后,按Ctrl+Alt+Home|Alt+Cmd+上箭头。你应该看到列出了activity_main.xml文件。使用你的方向键开选择然后回车。

菜�$�"�C�

Last Edit Location

最近的编辑位置

The Last Edit Location action Ctrl+Shift+Backspace | Cmd+Shift+Backspace allows you to navigate to your last edit. If you continue to activate this command, your cursor will move to the file/location of your previous edit, and so on.

Last Edit Lcation操作Ctrl+Shift+空格|Cmd+Shift+空格,这个操作允许你看到最近的编辑位置。如果你继续激活这个命令,你的光标将会移动的文件之前操作的位置,一直激活,光标一直移动。

Type Hierarchy

类型层次

Android uses Java, an object-oriented programming language. One of the hallmarks of any object-oriented language is inheritance, which facilitates code reuse and polymorphism. With the MainActivity.java file active in the Editor, press Ctrl+H to toggle open the Hierarchy tool window. There you will see a cascading series of objects, all of which can trace their ancestry to the progenitor of all objects in Java called Object. Keep in mind that the Navigate ➤ Type Hierarchy action will work only when the active tab in the Editor is a Java source file.

Android使用一种面向对象的编程语言——Java。任何一种面向对象的变成语言都具有的一个特点是继承,这是为了促进代码的重用和和多态。在编辑器中打开MainActivity.java文件,按Ctrl+H来打开层级工具床口。你将会看到一系列的级联对象,所有的对象都可以追踪它们的起源直到Java中的object对象。记住,Navigate->Type的类型层次操作只能在Java文件中进行

Declaration

声明操作

The Declaration action allows you to jump to the original definition of methods, variables, and resources. Another way to activate this action is by holding the Ctrl|Cmd key down while rolling your mouse over methods, variables, or resources in your file. If the element becomes underlined, you may navigate to its declaration by left-clicking the element while continuing to hold down the Ctrl|Cmd key. In MainActivity.java, click your cursor anywhere in the method setContentView(...) and press Ctrl+B | Cmd+B. You will be taken immediately to this method’s declaration, which is located in one of MainActivity’s superclasses called ActionBarActivity.java.

声明操作可以允许你直接跳跃到方法、变量、资源的定义的起始位置。另一个使用这个功能的方法是按住Ctrl|Cmd然后滚动鼠标。如果这个元素下面有了下划线,继续按住Ctrl|Cmd左击这个元素你可以找到这个元素的声明。在MainActivity.java中的setContentView()方法中的任意位置点击你的鼠标,然后按Ctrl+B|Cmd+B。你就会立刻到达这个方法的声明处。这个位置在一个叫ActionBarActivity.java的类中,它是MainActivity.java的一个子类。

Finding and Replacing Text

查找和替换文本

Finding and replacing text is an essential part of programming, and Android Studio has a powerful suite of tools to help you do just that. This section covers some of the most important tools. Table 2-4 lists them for you.

查和替换文本是编程中至关重要的一部分,Android Studio中有强大的一系列工具帮助你完成这个工作。这部分包含了很多重要的工具。如表2-4所示:

Table 2-4. Find and Replace


Command                                                PC Keys                                              Mac Keys

Find                                                        Ctrl+F                                                       Cmd+F

Find in Path                                          Ctrl+Shift+F                                            Cmd+Shift+F

Replace                                                   Ctrl+R                                                        Cmd+R

Replace in Path                                     Ctrl+Shift+R                                             Cmd+Shift+R


Find

查找

The Find action is used to find text occurrences within a single file. In MainActivity.java, press Ctrl+F | Cmd+F to bring up a search bar that appears along the top of the Editor. Type action in the search box of the search bar. You will notice that action is immediately highlighted in yellow throughout your file. You will also notice small green ticks in the marker bar indicating the locations of the found text. Rolling your mouse over the double rightarrows on the find bar will display advanced search options.

查找功能被用来在单个文件中查找文本。在MainActivity.java中,按Ctrl+F|Cmd+F来呼出查找栏,它出现在编辑器的顶端。在查找栏的查找对话框中键入具体操作,你将会注意到你的整个文件中这个操作后出现了黄色的高亮。你也将会发现在标记栏出现了绿色的十字表明了查找的文本的位置。将你鼠标指针放在查找栏旁边的两个箭头上就会显示优先查找的选项

Find in Path

路径中查找

The Find in Path action allows you to search in a much wider scope than with the Find action described previously. You can also use regular expressions, and delimit results with a file mask. Press Ctrl+Shift+F | Cmd+Shift+F and type hello in the search box of the search bar along the top of the Editor. By default, the search scope in Find in Path is set to Whole Project, though you can limit the search scope to a particular directory or module. Accept the default of Whole Project and click the Find button. The results appear in the Find tool window. Clicking an entry in the Find tool window immediately opens the enclosing file as a new tab of the Editor and jumps to that occurrence.

Find in Path操作允许你在一个比Find操作更加宽泛的范围中进行查找。你也可以使用正则表达式,使用文件遮蔽来界定查找结果。按Ctrl+Shift+F|Cmd+Shift+F之后再编辑器顶部出现的查找对话框中键入hello。默认的,Find in Path的查找范围是整个工程,当然你可以设置查找的范围是某个目录或者是某个模块。接受默认查找整个工程然后点击Find按钮。这个将会出现在Find工具窗口,在Find工具窗口总点击一个入口就会立即打开一个关闭的文件作为一个编辑器的新页面,然后跳到出现的页面。

Replace

代替

The Replace action Ctrl+R | Cmd+R is used to replace a text occurrence in a single file, and the functionality of Replace is a superset of Find. The safer way to replace text is to use the Refactor ➤ Rename command, which we will cover later.

代替操作Ctrl+R|Cmd+R被用在一个单文件中取代一个文本。这个取代的功能是Find的一个子功能。这种更加安全的取代文本的方式是这么操作的:Refactor->Rename,我们之后将会讲到这个。

Replace in Path

路径替换

The Replace in Path action Ctrl+Shift+R | Cmd+Shift+R is a superset of Find in Path. However, it’s almost always better to use Refactor ➤ Rename than to use Replace in Path, so use this command with extreme caution as you could introduce errors.

路径替换操作Ctrl+Shift+R|Cmd+Shift+R是Find in Path的一个超集。然而,似乎使用Refactor->Rename总比Replace in Path更加好用,因此使用这个命令要格外的小心,因为你可能会出现错误。

Summary

总结

In this chapter, we’ve discussed the Editor and the tool windows that cluster around the Editor. We’ve discussed how to use the tool buttons and reposition them. We’ve also discussed those tool windows that are used for navigation and the major UI elements of the IDE, including the main menu bar, the toolbar, the status bar, the gutter, and the marker bar. We’ve also discussed how to search and navigate by using menus and keyboard shortcuts, as well as using Find and Replace. Finally, we discussed how to use the help system in Android Studio. Most important, we’ve established a lexicon of UI elements in Android Studio to which we will refer in subsequent chapters.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,847评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,208评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,587评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,942评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,332评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,587评论 1 218
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,853评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,568评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,273评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,542评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,033评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,373评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,031评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,073评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,830评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,628评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,537评论 2 269

推荐阅读更多精彩内容