处理UI事件

   

为了让新的widget有交互性,它需要响应用户事件,如按键按下,屏幕触摸和按钮点击等。Android提供了一些虚的事件处理器来与用户输入交互:

 

onKeyDown

当设备按键被按下时调用;包括D-pad,键盘,挂机,呼叫,回退和照相按钮

 

onKeyUp

当用户释放按下的键时调用

 

onTrackballEvent

当轨迹球移动时调用

 

onTouchEvent

当触摸屏被按下或释放,或者检测到移动时调用

 

接下来的代码片段给了View中重写每个用户交互处理器的框架代码:

 

@Override

public boolean onKeyDown(int keyCode, KeyEvent keyEvent) {

// Return true if the event was handled.

return true;

}

 

@Override

public boolean onKeyUp(int keyCode, KeyEvent keyEvent) {

// Return true if the event was handled.

return true;

}

 

@Override

public boolean onTrackballEvent(MotionEvent event ) {

// Get the type of action this event represents

int actionPerformed = event.getAction();

// Return true if the event was handled.

return true;

}

 

@Override

public boolean onTouchEvent(MotionEvent event) {

// Get the type of action this event represents

int actionPerformed = event.getAction();

// Return true if the event was handled.

return true;

}

 

更多的关于每个事件处理器的细节以及这些方法的参数的细节,请看第11章。

你可能感兴趣的:(UI)