Responding to simple touches
One of the primary means for the user to interact with the device is through touch. Often, this is the only way as there is no keyboard or mouse, and other methods might be unavailable or undesirable.
How to do it...
All views provide access to the two most common forms of touch input: single taps and long presses. We respond to these events using either listeners or event handlers:
- We respond to taps using the
Click
event:view.Click += (sender, e) => { // the user tapped the view };
- In the same way, we respond to long presses using the
LongClick
event. To prevent theClick
event from also being triggered, we ensure that theHandled
property of theEventArgs
is set totrue
:view.LongClick += (sender, e) => { // the user long-pressed on the view e.Handled = true; };
Both the Click
and LongClick
events can also be subscribed to using listeners:
- To use a listener with the
Click
event, we ensure that we implement theView.IOnClickListener
interface:public class...