捕获当前触摸状态
package com.example.touchpostionshow;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.widget.EditText;
public class MainActivity extends Activity {
public EditText pox,poY,condition;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pox = (EditText)findViewById(R.id.editText1);
poY = (EditText)findViewById(R.id.editText2);
condition = (EditText)findViewById(R.id.editText3);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
float x = event.getX();
float y = event.getY();
try
{
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN: pox.setText(""+x);poY.setText(""+y);condition.setText("down");break;
case MotionEvent.ACTION_UP:pox.setText(""+x);poY.setText(""+y);condition.setText("up");break;
case MotionEvent.ACTION_MOVE:pox.setText(""+x);poY.setText(""+y);condition.setText("move");break;
}
return true;
}
catch(Exception e)
{
Log.v("touch", e.toString());
return false;
}
}
}
安卓涂鸦板原理讲解