I got a little problem. In the following sample the first TextView has to show the type of MotionEvent-it works fine. The second TextView has to show the coordinates of the MotionEvent-but it doesn't work. I don't know why but maybe its only a small error ? Does anyone has an idea? Thanks for your help! Here is the Code:
package de.androidnewcomer.motionevent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;
import static android.R.attr.x;
import static android.R.attr.y;
public class MainActivity extends AppCompatActivity implements View.OnTouchListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FrameLayout Spielbereich=(FrameLayout)findViewById(R.id.Spielbereich);
Spielbereich.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
TextView textView1=(TextView)findViewById(R.id.textView1);
TextView textView2=(TextView)findViewById(R.id.textView2);
TextView textView3=(TextView)findViewById(R.id.textView3);
TextView textView4=(TextView)findViewById(R.id.textView4);
int x1,x2,y1,y2;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
x1 = (int)event.getX();
y1 = (int)event.getY();
textView1.setText("Action Down");
textView2.setText(x1,y1);
return true;
}
case MotionEvent.ACTION_UP: {
x2 = (int)event.getX();
y2 = (int)event.getY();
textView3.setText("Action Up");
textView4.setText(x2,y2);
return true;
}
} return false;
}
}