B
B
BlaSSSTTT2022-01-30 13:12:53
Android
BlaSSSTTT, 2022-01-30 13:12:53

onTouchListener not working correctly in Android Studio, what should I do?

when you start the program, the message is displayed on the whole screen and all the buttons and fields that were before disappear, tell me what to do?

public class MainActivity extends AppCompatActivity implements OnTouchListener {
TextView tv;
float x;
float y;
String sDown;
String sMove;
String sUp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = new TextView(this);
tv.setOnTouchListener(this);
setContentView(new MyView(this));
 
}
 
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
x = motionEvent.getX();
y = motionEvent.getY();
 
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN: // нажатие
sDown = "Down: " + x + "," + y;
sMove = ""; sUp = "";
break;
case MotionEvent.ACTION_MOVE: // движение
sMove = "Move: " + x + "," + y;
break;
case MotionEvent.ACTION_UP: // отпускание
case MotionEvent.ACTION_CANCEL:
sMove = "";
sUp = "Up: " + x + "," + y;
break;
}
tv.setText(sDown + "\n" + sMove + "\n" + sUp);
 
return true;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AlexVWill, 2022-01-30
@AlexVWill

after setContentView(R.layout.activity_main);
you have another one, and it seems to distort your interface ... The way out is to register all the interface elements you need in R.layout.activity_main, and remove
setContentView(new MyView(this));
setContentView(new MyView(this));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question