150 likes | 338 Views
07.1. Event Handling. Prof. Oum Saokosal Master of Engineering in Information Systems, South Korea 855-12-252-752 oum_saokosal@yahoo.com. Agenda. XML-based Event Handling Java-based Event Handling More Event Listeners. XML-based Event Handling. XML-based Event Handling (1). 1.
E N D
07.1. Event Handling Prof. OumSaokosal Master of Engineering in Information Systems, South Korea 855-12-252-752 oum_saokosal@yahoo.com
Agenda XML-based Event Handling Java-based Event Handling More Event Listeners
XML-based Event Handling (1) 1 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <Button android:text="Show Me!" android:id="@+id/btnShowMe" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="showMe" /> </LinearLayout>
XML-based Event Handling (2) 2 packagecom.kosalab; importandroid.app.Activity; importandroid.os.Bundle; importandroid.view.View; importandroid.widget.Toast; publicclassWidgetContainerextends Activity { publicvoidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.eventhandling); } publicvoidshowMe(View v){ Toast.makeText(this, "This event invoked from XML.", Toast.LENGTH_SHORT).show(); } }
Java-based Event Handling (1) 1 ?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <Button android:text="Show Me!" android:id="@+id/btnShowMe" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
Java-based Event Handling (2) 4 2 3 5 publicclassWidgetContainerextends Activity implements OnClickListener{ Button btn; publicvoidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.eventhandling); btn = (Button)findViewById(R.id.btnShowMe); btn.setOnClickListener(this); } publicvoidonClick(View v) { Toast.makeText(this, "It's Toast Message! It appears shortly.“, Toast.LENGTH_SHORT).show(); } }
2 1 3 4 publicclassWidgetContainerextends Activity implementsOnClickListener, OnLongClickListener{ Button btn; publicvoidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.eventhandling); btn = (Button)findViewById(R.id.btnShowMe); btn.setOnClickListener(this); btn.setOnLongClickListener(this); } publicvoidonClick(View v) { Toast.makeText(this, “Short click!“, Toast.LENGTH_SHORT).show(); } publicbooleanonLongClick(View v) { Toast.makeText(this, “Long click!", Toast.LENGTH_SHORT).show(); returntrue; } }
onLongClick() – return true/false publicbooleanonLongClick(View v) { Toast.makeText(this, “Long click!", Toast.LENGTH_SHORT).show(); returntrue; } Note: return true: show only the longClick. return false: show both longclick and then short click.
More Event Listeners 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").