Single Touch Test : Touch « User Event « Android






Single Touch Test

   

package app.test;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;

public class Test extends Activity implements OnTouchListener {
  StringBuilder builder = new StringBuilder();
  TextView textView;

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    textView = new TextView(this);
    textView.setText("Touch and drag (one finger only)!");
    textView.setOnTouchListener(this);
    setContentView(textView);
  }

  @Override
  public boolean onTouch(View v, MotionEvent event) {
    builder.setLength(0);
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
      builder.append("down, ");
      break;
    case MotionEvent.ACTION_MOVE:
      builder.append("move, ");
      break;
    case MotionEvent.ACTION_CANCEL:
      builder.append("cancle, ");
      break;
    case MotionEvent.ACTION_UP:
      builder.append("up, ");
      break;
    }
    builder.append(event.getX());
    builder.append(", ");
    builder.append(event.getY());
    String text = builder.toString();
    Log.d("TouchTest", text);
    textView.setText(text);
    return true;
  }
}

   
    
    
  








Related examples in the same category

1.Report your Multitouch operation
2.Touch screen
3.Testing the Multitouch API
4.Demonstrates the handling of touch screen and trackball events to implement a simple painting app.
5.Demonstrates splitting touch events across multiple views within a view group.
6.Using your finger to draw