实验记录
活动一
package com.example.testone;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnTouchListener {
private TextView tvTouchShowStart;
private TextView tvTouchShow;
private LinearLayout llTouch;
private TextView tvTouchInfoSequence;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button)findViewById(R.id.change_activity2);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,MainActivity2.class);
startActivity(intent);
}
});
init();
}
private void init() {
tvTouchShowStart = (TextView) findViewById(R.id.touch_show_start);
tvTouchShow = (TextView) findViewById(R.id.touch_show);
llTouch = (LinearLayout) findViewById(R.id.ll_touch);
tvTouchInfoSequence = (TextView) findViewById(R.id.sequence);
llTouch.setOnTouchListener(this);
}
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction()) {
/**
* 点击的开始位置
*/
case MotionEvent.ACTION_DOWN:
tvTouchInfoSequence.setText("(" +event.getX() +"," +event.getY() +")\n" +event.getPressure() +"\n" +event.getSize() +"\n" +event.getDownTime());
tvTouchShowStart.setText("起始位置:(" + event.getX() + "," + event.getY());
break;
/**
* 触屏实时位置
*/
case MotionEvent.ACTION_MOVE:
tvTouchInfoSequence.setText("(" +event.getX() +"," +event.getY() +")\n" +event.getPressure() +"\n" +event.getSize() +"\n" +event.getDownTime());
tvTouchShow.setText("实时位置:(" + event.getX() + "," + event.getY());
break;
/**
* 离开屏幕的位置
*/
case MotionEvent.ACTION_UP:
tvTouchInfoSequence.setText("(" +event.getX() +"," +event.getY() +")\n" +event.getPressure() +"\n" +event.getSize() +"\n" +event.getDownTime());
tvTouchShow.setText("结束位置:(" + event.getX() + "," + event.getY());
break;
default:
break;
}
/**
* 注意返回值
* true:view继续响应Touch操作;
* false:view不再响应Touch操作,故此处若为false,只能显示起始位置,不能显示实时位置和结束位置
*/
return true;
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/ll_touch"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/change_activity2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="活动2"/>
<TextView
android:id="@+id/tips"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="(触屏坐标x,触屏坐标y || 触屏压力 || 触屏面积 || 触屏时间)"/>
<TextView
android:id="@+id/sequence"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/touch_show_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<TextView
android:id="@+id/touch_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</LinearLayout>
<!-- <LinearLayout-->
<!-- android:id="@+id/touch_xy"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="match_parent"-->
<!-- android:orientation="vertical">-->
<!-- <Button-->
<!-- android:id="@+id/button_1"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:text="启动活动2"/>-->
<!-- <TextView-->
<!-- android:id="@+id/tips"-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:text="(触屏坐标x,触屏坐标y || 触屏压力 || 触屏面积 || 触屏时间)"/>-->
<!-- <TextView-->
<!-- android:id="@+id/sequence"-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:text="@string/hello_world"/>-->
<!-- -->
<!-- </LinearLayout>-->
</androidx.constraintlayout.widget.ConstraintLayout>
效果: