Handling UI Events

VIEW

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. 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). onFocusChange()

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

onKey()

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

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). 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 inCreating Menus for more information.

// 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.setOnClickListener(mCorkyListener); } .

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 public void onClick(View v) { // do something when the button is clicked

onKeyDown(int, KeyEvent) - Called when a new key event occurs. onKeyUp(int, KeyEvent) - Called when a key up event occurs. onTrackballEvent(MotionEvent) - Called when a trackball motion event occurs. onTouchEvent(MotionEvent) - Called when a touch screen motion event occurs. onFocusChanged(boolean, int, Rect) - Called when the view gains or loses focus.

VIEW

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

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

• ViewParent.requestDisallowInterceptTouchEvent(boolean) - Call this upon a parent View to indicate that it should not intercept touch events withonInterce ptTouchEvent(MotionEvent).

<LinearLayout android:orientation="vertical" ... >

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

android:nextFocusUp="@+id/bottom" ... />

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

android:nextFocusDown="@+id/top" ... /> </LinearLayout>

0 0

Post a comment