gpt4 book ai didi

java - GestureDetector 不适用于新布局

转载 作者:行者123 更新时间:2023-12-01 08:46:39 26 4
gpt4 key购买 nike

我创建了标准手势检测器,它根据给定的手势更改 TextView 。当我的 XML 仅包含我希望更改的相对布局和 TextView 时,一切正常。然而,当我添加网格布局和一些图标时,手势检测器不再工作,只是没有像以前那样更改 TextView 。代码是;

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:id="@+id/Relative_Layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/homebackground"
tools:context="com.example.james.fitness.MainActivity"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true">

<GridLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="@+id/Grid1">

<TextView
android:id="@+id/someText"
android:text="sometext"
android:foregroundGravity="center_vertical|center_horizontal"
android:gravity="center_vertical|center"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleX="5"
android:scaleY="5"
android:textColor="@color/colorAccent" />

<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
app:srcCompat="@drawable/fitnessbutton"
android:id="@+id/fitnessbutton"
android:layout_row="1"
android:layout_column="1"
android:layout_columnWeight="1"
android:layout_gravity="fill_horizontal"
android:gravity="center"
android:paddingBottom="100dp" />

<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
app:srcCompat="@drawable/drinkbutton"
android:id="@+id/drinkbutton"
android:layout_column="2"
android:layout_row="1"
android:layout_columnWeight="1"
android:layout_gravity="fill_horizontal"
android:gravity="center"
android:paddingBottom="100dp" />

<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
app:srcCompat="@drawable/foodbutton"
android:id="@+id/foodbutton"
android:layout_column="3"
android:layout_row="1"
android:layout_columnWeight="1"
android:layout_gravity="fill_horizontal"
android:gravity="center"
android:paddingBottom="100dp" />
</GridLayout>
</RelativeLayout>

还有 Java

import android.app.Activity;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;



public class MainActivity extends Activity implements
GestureDetector.OnGestureListener,
GestureDetector.OnDoubleTapListener {


private TextView someText;
private GestureDetectorCompat gestureDetector;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

someText = (TextView)findViewById(R.id.someText);
this.gestureDetector = new GestureDetectorCompat(this, this);
gestureDetector.setOnDoubleTapListener(this);
}

@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
someText.setText("SINGLETAP");
return true;
}

@Override
public boolean onDoubleTap(MotionEvent e) {
someText.setText("DOUBLETAP");
return true;
}

@Override
public boolean onDoubleTapEvent(MotionEvent e) {
someText.setText("DOUBLETAPEVENT");
return true;
}

@Override
public boolean onDown(MotionEvent e) {
someText.setText("DOWN");
return true;
}

@Override
public void onShowPress(MotionEvent e) {
someText.setText("PRESS");

}

@Override
public boolean onSingleTapUp(MotionEvent e) {
someText.setText("SINGLETAPUP");
return true;
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
someText.setText("SCROLL");
return true;
}

@Override
public void onLongPress(MotionEvent e) {
someText.setText("LONGPRESS");
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
someText.setText("FLING");
return true;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
gestureDetector.onTouchEvent(event);
return false;

}

}

请注意,如果我注释掉网格布局和 ImageView ,则代码会再次更改 TextView 。我只能假设手势检测器现在埋在网格布局下方,但不确定?

最佳答案

尝试在布局中删除这些行,它们在您的情况下没有用

android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"

关于java - GestureDetector 不适用于新布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42613802/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com