Advertisement
Guest User

SingleTouchTest.java

a guest
Jun 10th, 2012
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.55 KB | None | 0 0
  1. package com.Bespoke.AndroidBasics;
  2.  
  3. import android.app.Activity;
  4. import android.content.res.Configuration;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.view.MotionEvent;
  8. import android.view.View;
  9. import android.view.View.OnTouchListener;
  10. import android.widget.TextView;
  11.  
  12. public class SingleTouchTest extends Activity
  13.                              implements OnTouchListener {
  14.    
  15.     StringBuilder builder = new StringBuilder();
  16.     TextView textView;
  17.    
  18.     @Override
  19.     public void onCreate(Bundle savedInstanceState) {
  20.         super.onCreate(savedInstanceState);
  21.         textView = new TextView(this);
  22.         textView.setText("Touch and drag (one finger only!)");
  23.         textView.setOnTouchListener(this);
  24.         this.setContentView(textView);
  25.     }
  26.    
  27.     public boolean onTouch(View v, MotionEvent event) {
  28.         builder.setLength(0);  // clear the builder
  29.         switch(event.getAction()) {
  30.         case MotionEvent.ACTION_DOWN:
  31.             builder.append("down, ");
  32.             break;
  33.         case MotionEvent.ACTION_MOVE:
  34.             builder.append("move, ");
  35.             break;
  36.         case MotionEvent.ACTION_CANCEL:
  37.             builder.append("cancel, ");
  38.             break;
  39.         case MotionEvent.ACTION_UP:
  40.             builder.append("up, ");
  41.             break;
  42.         }
  43.         builder.append(event.getX());
  44.         builder.append(", ");
  45.         builder.append(event.getY());
  46.         String text = builder.toString();
  47.         Log.d("TouchTest", text);
  48.         textView.setText(text);
  49.         return true;  // Consume the event, if false super.onTouch superceeds us
  50.     }
  51.    
  52.      @Override
  53.         public void onConfigurationChanged(Configuration  newConfig) {
  54.           super.onConfigurationChanged(newConfig);
  55.        
  56.         }
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement