您的位置:首页 > 移动开发 > Android开发

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

2012-02-04 15:58 465 查看
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.OnFocusChangeListener
onFocusChange()
 
View.OnKeyListener
.
onKey()
 
View.OnTouchListener
onTouch()
View.OnCreateContextMenuListeneronCreateContextMenu()
例子:

// Create an anonymous implementation of 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
    button.setOnClickL
d194
istener(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 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.

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

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

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

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, call
setFocusable()
.
When in touch mode, you may query whether a View allows focus with 
isFocusableInTouchMode()
.
You can change this with
setFocusableInTouchMode()
.

调用isFocusable()是否可以接收光标;setFocusable()设置;在xml中通过 
android:focusable
 设定。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息