android触摸实现方式
package com.ray.touch;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.TextView;
import android.widget.Toast;
public class TouchPaint extends Activity {
??? TextView tv;
??? @Override
??? public void onCreate(Bundle savedInstanceState) {
??????? super.onCreate(savedInstanceState);
??????? setContentView(R.layout.main);
??????? tv = (TextView) findViewById(R.id.tv);
??? }
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch(event.getAction()){
case MotionEvent.ACTION_DOWN: tv.setText("down"+"("+x+","+y+")");break;
case MotionEvent.ACTION_UP: tv.setText("up"+"("+x+","+y+")");break;
case MotionEvent.ACTION_MOVE:tv.setText("move"+"("+x+","+y+")");break;
}
return super.onTouchEvent(event);
}
}
package com.ray.touch;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.TextView;
import android.widget.Toast;
public class TouchPaint extends Activity {
??? TextView tv;
??? @Override
??? public void onCreate(Bundle savedInstanceState) {
??????? super.onCreate(savedInstanceState);
??????? setContentView(R.layout.main);
??????? tv = (TextView) findViewById(R.id.tv);
??? }
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch(event.getAction()){
case MotionEvent.ACTION_DOWN: tv.setText("down"+"("+x+","+y+")");break;
case MotionEvent.ACTION_UP: tv.setText("up"+"("+x+","+y+")");break;
case MotionEvent.ACTION_MOVE:tv.setText("move"+"("+x+","+y+")");break;
}
return super.onTouchEvent(event);
}
}