`

【翻译】(14)输入事件

 
阅读更多

【翻译】(14)输入事件

 

see

http://developer.android.com/guide/topics/ui/ui-events.html

 

原文见

http://developer.android.com/guide/topics/ui/ui-events.html

 

-------------------------------

 

Input Events

 

输入事件

 

-------------------------------

 

In this document

 

本文目录

 

* Event Listeners 事件监听器

* Event Handlers 事件句柄

* Touch Mode 触碰模式

* Handling Focus 处理焦点

 

Related tutorials 

 

相关教程

 

Form Stuff 表单材料

 

(注:此文并没有提及类android.view.GestureDetector的使用,它可以通过接管onTouch

public boolean onTouch(View v, MotionEvent event) {  

return detector.onTouchEvent(event);  

}  

来监听更复杂的手势

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {  

Log.i("MyGesture", "onFling");  

}

 

-------------------------------

 

On Android, there's more than one way to intercept the events from a user's interaction with your application. When considering events within your user interface, the approach is to capture the events from the specific View object that the user interacts with. The View class provides the means to do so.

 

在Android上,有多于一种方法拦截用户与你的应用程序交互的事件。当感兴趣的事件发生在你的用户界面时,方法是从指定与用户交互的View对象中捕获事件。View类提供一些方法来做到这一点。

 

Within the various View classes that you'll use to compose your layout, you may notice several public callback methods that look useful for UI events. These methods are called by the Android framework when the respective action occurs on that object. For instance, when a View (such as a Button) is touched, the onTouchEvent() method is called on that object. However, in order to intercept this, you must extend the class and override the method. However, extending every View object in order to handle such an event would not be practical. This is why the View class also contains a collection of nested interfaces with callbacks that you can much more easily define. These interfaces, called event listeners, are your ticket to capturing the user interaction with your UI.

 

在你用来组合你的布局的不同View类中,你可能会注意到几个公共的回调方法,看起来对于用户界面事件有用。当相应的动作发生在那个对象上时,这些方法被Android框架调用。例如,当一个View(诸如一个Button)被触碰时,onTouchEvent()方法被调用在那个对象上。然而为了拦截这个事件,你必须扩展这个类并覆盖这个方法。然而,扩展每个View对象以处理该事件是不切实际的。因此View类还包含一系列带回调的内嵌接口类,你可以更容易地定义它们。这些接口类,称为事件监听器,是你捕获用户与你的用户界面交互的关键。

 

While you will more commonly use the event listeners to listen for user interaction, there may come a time when you do want to extend a View class, in order to build a custom component. Perhaps you want to extend the Button class to make something more fancy. In this case, you'll be able to define the default event behaviors for your class using the class event handlers.

 

虽然你将更通常地使用事件监听器来监听用户交互,但是可能有时候你希望扩展一个View类,以构建一个自定义组件。可能你希望扩展Button类以使某些东西变得更棒。在这种情况下,你可以使用类事件句柄为你的类定义默认事件行为。

 

-------------------------------

 

Event Listeners

 

事件监听器

 

An event listener is an interface in the View class that contains a single callback method. These methods will be called by the Android framework when the View to which the listener has been registered is triggered by user interaction with the item in the UI.

 

事件监听器是View类的接口类,包含单一回调方法。当已经注册了监听器的View被用户与用户界面中条目的交互触发时,这些方法被Android框架调用。

 

Included in the event listener interfaces are the following callback methods:

 

包含在事件监听器接口类中的回调方法如下所示:

 

* onClick()

 

From View.OnClickListener. This is called when the user either touches the item (when in touch mode), or focuses upon the item with the navigation-keys or trackball and presses the suitable "enter" key or presses down on the trackball.

 

来自View.OnClickListener。此回调在用户触碰条目(触碰模式下),或用导航键或轨迹球把焦点移到条目上并按下合适的“进入”键或按下轨迹球时被调用。

 

* onLongClick()

 

From View.OnLongClickListener. This is called when the user either touches and holds the item (when in touch mode), or focuses upon the item with the navigation-keys or trackball and presses and holds the suitable "enter" key or presses and holds down on the trackball (for one second).

 

来自View.OnLongClickListener。此回调在用户触碰并按住该条目(触碰模式下),或用导航键或轨迹球把焦点移到条目上并按下和按住合适的“进入”键或按下并按住轨迹球(持续一秒)时被调用。

 

* onFocusChange()

 

From View.OnFocusChangeListener. This is called when the user navigates onto or away from the item, using the navigation-keys or trackball.

 

来自View.OnFocusChangeListener。此回调在用户使用导航键或轨迹球导航进入或离开条目时被调用。

 

* onKey()

 

From View.OnKeyListener. This is called when the user is focused on the item and presses or releases a key on the device.

 

来自View.OnKeyListener。它在用户焦点移进条目并在设备上按下或释放按键时被调用。

 

* onTouch()

 

From View.OnTouchListener. This is called when the user performs an action qualified as a touch event, including a press, a release, or any movement gesture on the screen (within the bounds of the item).

 

来自View.OnTouchListener。此回调在用户执行一个被认为是触碰事件的动作时被调用,包括在屏幕上按下,释放,或任意移动手势(在该条目的范围内)。

 

* onCreateContextMenu()

 

From View.OnCreateContextMenuListener. This is called when a Context Menu is being built (as the result of a sustained "long click"). See the discussion on context menus in the Menus developer guide.

 

来自View.OnCreateContextMenuListener。此回调在一个上下文菜单被构建(作为持续“长点击”的结果)时被调用。参见菜单开发者指引中关于上下文菜单的讨论。

 

These methods are the sole inhabitants of their respective interface. To define one of these methods and handle your events, implement the nested interface in your Activity or define it as an anonymous class. Then, pass an instance of your implementation to the respective View.set...Listener() method. (E.g., call setOnClickListener() and pass it your implementation of the OnClickListener.)

 

这些方法是它们对应接口类的唯一寄居物。为了定义这些方法中的一个并处理你的事件,请在你的Activity中实现嵌套接口或定义它作为一个匿名类。然后传递你的实现的一个实例给相应的View.set...Listener()方法。(例如,调用setOnClickListener()并把你的OnClickListener的实现传递给它。)

 

The example below shows how to register an on-click listener for a Button.

 

下面的例子展示如何注册一个Button对象的点击监听器。

 

-------------------------------

 

// Create an anonymous implementation of OnClickListener

// 创建OnClickListener的匿名实现

private OnClickListener mCorkyListener = new OnClickListener() {

    public void onClick(View v) {

      // do something when the button is clicked

      // 在按钮点击时执行一些操作

    }

};

 

protected void onCreate(Bundle savedValues) {

    ...

    // Capture our button from layout

    // 从布局中取出我们的按钮

    Button button = (Button)findViewById(R.id.corky);

    // Register the onClick listener with the implementation above

    // 注册上面实现的onClick监听器

    button.setOnClickListener(mCorkyListener);

    ...

}

 

-------------------------------

 

You may also find it more convenient to implement OnClickListener as a part of your Activity. This will avoid the extra class load and object allocation. For example:

 

你还可以发现实现OnClickListener作为你的Activity类的一部分会更为便利。这样将避免额外的类加载和对象分配。例如:

 

-------------------------------

 

public class ExampleActivity extends Activity implements OnClickListener {

    protected void onCreate(Bundle savedValues) {

        ...

        Button button = (Button)findViewById(R.id.corky);

        button.setOnClickListener(this);

    }

 

    // Implement the OnClickListener callback

    // 实现OnClickListener回调

    public void onClick(View v) {

      // do something when the button is clicked

      // 在按钮点击时执行一些操作

    }

    ...

}

 

-------------------------------

 

Notice that the onClick() callback in the above example has no return value, but some other event listener methods must return a boolean. The reason depends on the event. For the few that do, here's why:

 

注意上面实例代码中的onClick()回调没有返回值,但其他一些事件监听器方法必须返回一个布尔值。理由依赖于事件。对于少数要这样做的情况,理由是:

 

* onLongClick() - This returns a boolean to indicate whether you have consumed the event and it should not be carried further. That is, return true to indicate that you have handled the event and it should stop here; return false if you have not handled it and/or the event should continue to any other on-click listeners.

 

* onLongClick() - 此回调返回布尔值以指出你是否已经消耗事件而它是否应该搬运得更远。具体来说,返回true指示你已经处理事件而它应该在这里停止;返回false,如果你还没有处理它和/或事件应该继续传给其它任意点击监听器。

 

* onKey() - This returns a boolean to indicate whether you have consumed the event and it should not be carried further. That is, return true to indicate that you have handled the event and it should stop here; return false if you have not handled it and/or the event should continue to any other on-key listeners.

 

* onKey() - 此回调返回一个布尔值指示你是否已经消耗事件,并且它不应该被搬运到更远。具体来说,返回true表示你已经处理事件而它应该在这里停止;返回false,如果你还没有处理它和/或事件应该继续传给其他任意的键盘按下监听器。

 

* onTouch() - This returns a boolean to indicate whether your listener consumes this event. The important thing is that this event can have multiple actions that follow each other. So, if you return false when the down action event is received, you indicate that you have not consumed the event and are also not interested in subsequent actions from this event. Thus, you will not be called for any other actions within the event, such as a finger gesture, or the eventual up action event.

 

* onTouch() - 此回调返回一个布尔值指示你的监听器是否消耗此事件。重要的事情是此事件可以拥有多个互相跟随的动作。所以,如果当按下动作事件接收时你返回false,那么你表示你还没有消耗该事件,并且不关心这个事件随后的动作。这样,你将不会因为这个事件中的其他任意动作而被调用,诸如手指动作,或最终的向上动作事件。

 

Remember that key events are always delivered to the View currently in focus. They are dispatched starting from the top of the View hierarchy, and then down, until they reach the appropriate destination. If your View (or a child of your View) currently has focus, then you can see the event travel through the dispatchKeyEvent() method. As an alternative to capturing key events through your View, you can also receive all of the events inside your Activity with onKeyDown() and onKeyUp().

 

记住键盘事件总是被传递给当前获取焦点的View。它们从View层级的顶部开始被派发,然后向下,直至它们到达合适的目标。如果你的视图(或你的视图的子对象)当前拥有焦点,那么你可以看到事件通过dispatchKeyEvent()方法传播。作为通过你的View捕获键盘事件的替换方案,你还可以在你的Activity中用onKeyDown()和onKeyUp()接收所有事件。

 

-------------------------------

 

Note: Android will call event handlers first and then the appropriate default handlers from the class definition second. As such, returning true from these event listeners will stop the propagation of the event to other event listeners and will also block the callback to the default event handler in the View. So be certain that you want to terminate the event when you return true.

 

注意:Android将首先调用事件句柄然后第二步才从类定义中调用合适的默认句柄。因此,从这些事件监听器中返回true将阻止事件传播给其它事件监听器并且还将阻塞对View中默认事件句柄的回调。所以请肯定当你返回true时你想终止事件。

 

-------------------------------

 

Event Handlers

 

事件句柄

 

If you're building a custom component from View, then you'll be able to define several callback methods used as default event handlers. In the document about Custom Components, you'll learn see some of the common callbacks used for event handling, including:

 

如果你正在从View中构建一个自定义组件,那么你将可以定义几个用作默认事件句柄的回调方法。在关于自定义组件的文档中,你将看到一些通常用于事件处理的回调,包括:

 

* onKeyDown(int, KeyEvent) - Called when a new key event occurs.

 

* onKeyDown(int, KeyEvent) - 当新的按键事件发生时被调用。

 

* onKeyUp(int, KeyEvent) - Called when a key up event occurs.

 

* onKeyUp(int, KeyEvent) - 当一个按键弹起事件发生时被调用。

 

* onTrackballEvent(MotionEvent) - Called when a trackball motion event occurs.

 

* onTrackballEvent(MotionEvent) - 当轨迹球动作事件发生时被调用。

 

* onTouchEvent(MotionEvent) - Called when a touch screen motion event occurs.

 

* onTouchEvent(MotionEvent) - 当一个触碰屏幕动作事件发生时被调用。

 

* onFocusChanged(boolean, int, Rect) - Called when the view gains or loses focus.

 

* onFocusChanged(boolean, int, Rect) - 当视图获取或失去焦点时被调用。

 

There are some other methods that you should be aware of, which are not part of the View class, but can directly impact the way you're able to handle events. So, when managing more complex events inside a layout, consider these other methods:

 

你应该意识到其它几个方法,它们不是View类的一部分,但可以直接影响你能处理事件的方式。所以,当管理布局中更复杂事件时,请考虑其它这些方法:

 

* Activity.dispatchTouchEvent(MotionEvent) - This allows your Activity to intercept all touch events before they are dispatched to the window.

 

* Activity.dispatchTouchEvent(MotionEvent) - 此回调允许在所有触碰事件被派发到窗口之前,你的Activity拦截它们。

 

* ViewGroup.onInterceptTouchEvent(MotionEvent) - This allows a ViewGroup to watch events as they are dispatched to child Views.

 

* ViewGroup.onInterceptTouchEvent(MotionEvent) - 此回调允许一个ViewGroup在事件被派发到子视图时监视它们。

 

* ViewParent.requestDisallowInterceptTouchEvent(boolean) - Call this upon a parent View to indicate that it should not intercept touch events with onInterceptTouchEvent(MotionEvent).

 

* ViewParent.requestDisallowInterceptTouchEvent(boolean) - 在父View上调用此回调以指示它不应该用onInterceptTouchEvent(MotionEvent)拦截触碰事件。

 

-------------------------------

 

Touch Mode

 

触碰模式

 

When a user is navigating a user interface with directional keys or a trackball, it is necessary to give focus to actionable items (like buttons) so the user can see what will accept input. If the device has touch capabilities, however, and the user begins interacting with the interface by touching it, then it is no longer necessary to highlight items, or give focus to a particular View. Thus, there is a mode for interaction named "touch mode."

 

当用户正在用方向键或轨迹球导航用户界面时,需要把焦点交给可活动的条目(如按钮),这样用户可以看到接收输入的是什么。然而,如果设备拥有触碰功能,而用户通过触碰它开始与界面交互,那么它不再需要高亮条目,或把焦点交给一个特定的View。因此,存在一种被称为“触碰模式”的交互模式。

 

For a touch-capable device, once the user touches the screen, the device will enter touch mode. From this point onward, only Views for which isFocusableInTouchMode() is true will be focusable, such as text editing widgets. Other Views that are touchable, like buttons, will not take focus when touched; they will simply fire their on-click listeners when pressed.

 

对于可捕获触碰的设备来说,一旦用户触碰屏幕,设备将进入触碰模式。从这一点开始,只有isFocusableInTouchMode()是true的View将是可获取焦点的,诸如文本编辑部件。其它可触碰的View,如按钮,在触碰时将不会获取焦点;它们简单地在按下时发送事件到它们的点击监听器。

 

Any time a user hits a directional key or scrolls with a trackball, the device will exit touch mode, and find a view to take focus. Now, the user may resume interacting with the user interface without touching the screen.

 

任何时候一个用户碰到一个方向键或用轨迹球滚动,设备都将推出触碰模式,并且寻找视图获取焦点。现在,用户可以恢复与用户界面的交互而不需要触碰屏幕。

 

The touch mode state is maintained throughout the entire system (all windows and activities). To query the current state, you can call isInTouchMode() to see whether the device is currently in touch mode.

 

触碰模式状态由整个系统维护(所有窗口和活动)。为了查询当前状态,你可以定义isInTouchMode()看看设备现在是否处于触碰模式中。

 

-------------------------------

 

Handling Focus

 

处理焦点

 

The framework will handle routine focus movement in response to user input. This includes changing the focus as Views are removed or hidden, or as new Views become available. Views indicate their willingness to take focus through the isFocusable() method. To change whether a View can take focus, call setFocusable(). When in touch mode, you may query whether a View allows focus with isFocusableInTouchMode(). You can change this with setFocusableInTouchMode().

 

框架将处理路由焦点移动以响应用户的输入。它包括当View被移除或隐藏时改变焦点,或当新的View变得可用时。View通过isFocusable()方法指示它们获取焦点的意愿。为了改变一个View是否可以获取焦点,可以调用setFocusable()。当在触碰模式下,你可以用isFocusableInTouchMode()查询一个View是否允许获取焦点。你可以用setFocusableInTouchMode()改变它。

 

Focus movement is based on an algorithm which finds the nearest neighbor in a given direction. In rare cases, the default algorithm may not match the intended behavior of the developer. In these situations, you can provide explicit overrides with the following XML attributes in the layout file: nextFocusDown, nextFocusLeft, nextFocusRight, and nextFocusUp. Add one of these attributes to the View from which the focus is leaving. Define the value of the attribute to be the id of the View to which focus should be given. For example:

 

焦点移动是基于一种算法,它查找给定方向上的最近邻居。在很少情况下,默认算法可能不匹配开发者的期待的行为。在这些情况下,你可以用以下布局文件的XML属性提供显式的覆盖:nextFocusDown,nextFocusLeft,nextFocusRight,和nextFocusUp。添加这些属性的其中一个到焦点要离开的View。定义属性的值为将要给予焦点的View的id。例如:

 

-------------------------------

 

<LinearLayout

    android:orientation="vertical"

    ... >

  <Button android:id="@+id/top"

          android:nextFocusUp="@+id/bottom"

          ... />

  <Button android:id="@+id/bottom"

          android:nextFocusDown="@+id/top"

          ... />

</LinearLayout>

 

-------------------------------

 

Ordinarily, in this vertical layout, navigating up from the first Button would not go anywhere, nor would navigating down from the second Button. Now that the top Button has defined the bottom one as the nextFocusUp (and vice versa), the navigation focus will cycle from top-to-bottom and bottom-to-top.

 

通常,在这个垂直布局中,从第一个Button向上导航将不会跑到其他地方,从第二个Button开始向下导航也不会这样。现在,顶部Button已经定义为底部Button的nextFocusUp(等等),导航焦点将循环地从上到下和从下到上。

 

If you'd like to declare a View as focusable in your UI (when it is traditionally not), add the android:focusable XML attribute to the View, in your layout declaration. Set the value true. You can also declare a View as focusable while in Touch Mode with android:focusableInTouchMode.

 

如果你喜欢声明View在你的用户界面中是可获取焦点的(当它传统上不可以),可以在布局声明中添加android:focusable的XML属性到那个View。设置值为true。你还可以用android:focusableInTouchMode声明一个View在触碰模式下是可获取焦点的,

 

To request a particular View to take focus, call requestFocus().

 

为了要求特定View对象获取焦点,请调用requestFocus()。

 

To listen for focus events (be notified when a View receives or looses focus), use onFocusChange(), as discussed in the Event Listeners section, above.

 

为了监听焦点事件(当一个View接收或失去焦点时被通知),使用onFocusChange(),正如上面在事件监听器章节中讨论的那样。

 

Except as noted, this content is licensed under Apache 2.0. For details and restrictions, see the Content License.

 

除特别说明外,本文在Apache 2.0下许可。细节和限制请参考内容许可证。

 

Android 4.0 r1 - 17 Nov 2011 21:59

Site Terms of Service - Privacy Policy - Brand Guidelines

 

-------------------------------

 

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

 

(此页部分内容基于Android开源项目,以及使用根据创作公共2.5来源许可证描述的条款进行修改)


分享到:
评论

相关推荐

    J2ME的MIDP游戏开发毕业论文

    目 录 摘 要 I Abstract II 第1章 绪 论 1 第2章 J2ME技术概览 2 2.1 J2ME的概念 2 2.2 J2ME简介 2 2.2.1 J2ME架构 3 2.2.2 JVM ( 虚拟机 ) 3 2.2.3 MIDP( 移动信息设备简表 ) 4 ...英文资料翻译 38

    STM32F10xxx参考手册

    南京万利提供的原始翻译文档. 本译文仅供参考,如有翻译错误,请以英文原稿为准。请读者随时注意在ST网站下载更新版本. 本译文仅供参考,如有翻译错误,请以英文原稿为准。请读者随时注意在ST网站下载更新版本 目录...

    C++ GUI Qt4编程(第二版).pdf【第一部分】

    前5章主要涉及qt基础知识,后两个部分主要讲解qt的中高级编程,包括布局管理、事件处理、二维/三维图形、拖放、项视图类、容器类、输入/输出、数据库、多线程、网络、xml、国际化、嵌入式编程等内容。对于本书讲授的...

    C++ GUI Qt4编程(第二版).pdf【第二部分】

    前5章主要涉及qt基础知识,后两个部分主要讲解qt的中高级编程,包括布局管理、事件处理、二维/三维图形、拖放、项视图类、容器类、输入/输出、数据库、多线程、网络、xml、国际化、嵌入式编程等内容。对于本书讲授的...

    C++ GUI Qt4编程(第二版).pdf【第三部分】

    前5章主要涉及qt基础知识,后两个部分主要讲解qt的中高级编程,包括布局管理、事件处理、二维/三维图形、拖放、项视图类、容器类、输入/输出、数据库、多线程、网络、xml、国际化、嵌入式编程等内容。对于本书讲授的...

    ARCH4系统开发指南

    2.18 事件处理机制 34 2.18.1 使用Observer模式 34 2.18.2 程序修改方式 34 2.19 用AJAX获取数据(通用做法) 36 2.19.1 “查看条款内容”的操作情景 36 2.19.2 前台JS函数调用后台JAVA类 36 2.19.3 后台JAVA类为...

    WPF编程宝典 part1

    5.3.2 输入事件 108 5.4 键盘输入 108 5.4.1 处理按键事件 109 5.4.2 焦点 111 5.4.3 获取键盘状态 112 5.5 鼠标输入 113 5.5.1 鼠标单击 115 5.5.2 捕获鼠标 115 5.5.3 鼠标拖放 116 5.6 多点触控输入 118 5.6.1 ...

    1105741847整合脚本(RMXP)

    20·价格变动 翻译:柳柳 21·菜单显示血条 22·物品分类 可以自定义设置物品分类,利用物品描述,每个描述后面添加@物品分类即可。 23·任务和菜单 by 柳柳 24·完美输入法(具体看脚本里的长篇说明) 25·显示敌人...

    WPF编程宝典 part2

    5.3.2 输入事件 108 5.4 键盘输入 108 5.4.1 处理按键事件 109 5.4.2 焦点 111 5.4.3 获取键盘状态 112 5.5 鼠标输入 113 5.5.1 鼠标单击 115 5.5.2 捕获鼠标 115 5.5.3 鼠标拖放 116 5.6 多点触控输入 118 5.6.1 ...

    PT80-NEAT开发指南v1.1

    事件驱动和消息响应机制 ..................................................................................................................... 17 建立一个应用程序 ..........................................

    一款非常好的WPF编程宝典2010 源代码

    6.3.2 输入事件 139 6.3.3 键盘输入 140 6.3.4 鼠标输入 145 6.4 结束语 149 第7章 经典控件 151 7.1 控件类 151 7.1.1 背景画刷和前景画刷 151 7.1.2 字体 155 7.1.3 鼠标光标 159 7.2 内容控件 160 ...

    Apress.Pro.WPF.in.C.Sharp.2008.2nd.Edition.Feb.2008

    6.3.2 输入事件 139 6.3.3 键盘输入 140 6.3.4 鼠标输入 145 6.4 结束语 149 第7章 经典控件 151 7.1 控件类 151 7.1.1 背景画刷和前景画刷 151 7.1.2 字体 155 7.1.3 鼠标光标 159 7.2 内容控件 160 7.2.1 标签 160...

    QT 3.3 中文白皮书

    作者说是翻词典给翻译出来的,对学QT编程的人应该有些帮助. 目录 1 介绍 …………………………………………………………………………………………4 1.1 执行摘要………………………………………………………………...

    QT3.3中文白皮书

    作者说是翻词典给翻译出来的,对学QT编程的人应该有些帮助. 目录 1 介绍 …………………………………………………………………………………………4 1.1 执行摘要…………………………………………………………...

    精通AngularJS part1

    14总结41 第2章构建与测试43 21介绍示例应用44 熟悉问题领域44 技术栈45 持久化存储46 MongoLab46 服务器端环境47 第三方JavaScript库48 BootstrapCSS48 22构建系统48 构建系统准则49 自动化所有事情49 ...

    C#语言参考C#语言参考

    2.1 翻译的阶段 37 2.2 文法符号 37 2.3 预处理 38 2.3.1 预处理声明 38 2.3.2 #if, #elif, #else, #endif 39 2.3.3 预处理控制行 40 2.3.4 #line 41 2.3.5 预处理标识符 41 2.3.6 预处理表达式 41 2.3.7 与空白交互...

Global site tag (gtag.js) - Google Analytics