5.01
所花时间(包括上课):2
打码量(行):250
博客量(篇):1
了解到知识点:学习手势的识别
<!-- res/layout/activity_main.xml -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/gestureTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Waiting for gestures..."
android:layout_centerInParent="true"
android:textSize="18sp"/>
</RelativeLayout>
// src/main/java/com/example/gestureapp/MainActivity.java
package com.example.gestureapp;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener {
private TextView gestureTextView;
private GestureDetector gestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gestureTextView = findViewById(R.id.gestureTextView);
gestureDetector = new GestureDetector(this, this);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
@Override
public boolean onDown(MotionEvent e) {
gestureTextView.setText("onDown");
return true;
}
@Override
public void onShowPress(MotionEvent e) {
gestureTextView.setText("onShowPress");
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
gestureTextView.setText("onSingleTapUp");
return true;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
gestureTextView.setText("onScroll");
return true;
}
@Override
public void onLongPress(MotionEvent e) {
gestureTextView.setText("onLongPress");
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
gestureTextView.setText("onFling");
return true;
}
}
<!-- src/main/AndroidManifest.xml -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gestureapp">
<application
android:allowBackup="true"
android:label="Gesture App"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.DarkActionBar">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
本文来自博客园,作者:赵千万,转载请注明原文链接:https://www.cnblogs.com/zhaoqianwan/p/18138355
千万千万赵千万