Android 开发指南 翻译3:User Interface: Input Events

来源:互联网 发布:阿里云最便宜的主机 编辑:程序博客网 时间:2024/06/06 03:42

Android 开发指南 翻译3:User Interface:Input Events


These methods are called by the Android framework when the respective action occurs on that object. 


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的一个接口,包含一个回调方法。当注册了监听器的视图与用户进行交互时,这些回调方法将被Android调用。

View.onClickListeneronClick()View.OnLongClickListeneronLongClick()View.OnFocusChangeListeneronFocusChange() View.OnKeyListener.onKey() View.OnTouchListeneronTouch()View.OnCreateContextMenuListeneronCreateContextMenu()例子:

// Create an anonymous implementation of OnClickListenerprivate 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    button.setOnClickListener(mCorkyListener);    ...}

Remember that key events are always delivered to the View currently in focus. 

key事件总是分配给当前获得光标的View。

Event Handlers

Touch Mode

For a touch-capable device, once the user touches the screen, the device will enter touch mode. From this point onward, only Views for whichisFocusableInTouchMode() 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.

非触摸模式:显示获得焦点的控件,以告知用户当前的焦点。

触摸模式:只有部分控件获得焦点时,才显示给用户。

对与可触摸的设备,用户一旦触摸屏幕,就进入触摸模式。

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.

用户只要使用方向键或滚球,系统退出触摸模式。

Handling Focus

Views indicate their willingness to take focus through the isFocusable() method. To change whether a View can take focus, callsetFocusable(). When in touch mode, you may query whether a View allows focus with isFocusableInTouchMode(). You can change this withsetFocusableInTouchMode().

调用isFocusable()是否可以接收光标;setFocusable()设置;在xml中通过 android:focusable 设定。



原创粉丝点击