gpt4 book ai didi

java - OnTouchListener 没有被解雇

转载 作者:行者123 更新时间:2023-12-01 14:12:00 27 4
gpt4 key购买 nike

请看下面的代码

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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ParagraphReader" >

<ScrollView
android:id="@+id/paragraph_reader_scroll_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/paragraph_reader_txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="30"
android:singleLine="false"
android:enabled="true"
>


</TextView>

</ScrollView>






</RelativeLayout>

Java 代码

package k.k;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View.OnTouchListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class ParagraphReader extends Activity {

private TextView paraText;
private DatabaseConnector database;
private List<String>paraList;
private int currentQuestion;

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

paraText = (TextView)findViewById(R.id.paragraph_reader_txt);
paraText.setOnTouchListener(paraSwiped);

paraList = new ArrayList<String>();
database = DatabaseHandler.getInstance(this);

//Get the Paragraph list
int listNumber = getIntent().getIntExtra("PARAGRAPH_LIST", 0);

Toast.makeText(this, "Selected Paragraph: "+listNumber, Toast.LENGTH_LONG).show();

paraList = database.getParagraphList(listNumber);
Toast.makeText(this, "ParaList size "+paraList.size(), Toast.LENGTH_LONG).show();

//Toast.makeText(this, "Size: "+paraList.size(), Toast.LENGTH_LONG).show();
paraText.setText(paraList.get(0));

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.paragraph_reader, menu);
return true;
}


//The Event Handler for the Paragraph Text holder
OnTouchListener paraSwiped = new OnSwipeTouchListener()
{
public boolean onSwipeRight()
{
Toast.makeText(ParagraphReader.this, "Right: "+paraList.size(), Toast.LENGTH_SHORT).show();


return true;
}

public boolean onSwipeLeft()
{
Toast.makeText(ParagraphReader.this, "Left: "+paraList.size(), Toast.LENGTH_SHORT).show();

return true;
}


};

}

在这里,您可以看到我已经为 TextView 实现了一个 OnTouchListener。下面是 OnTouchListener 类的代码。此代码由 SO 成员之一构建。

package k.k;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class OnSwipeTouchListener implements OnTouchListener {

private final GestureDetector gestureDetector = new GestureDetector(new GestureListener());

public boolean onTouch(final View v, final MotionEvent event) {
return gestureDetector.onTouchEvent(event);

}

private final class GestureListener extends SimpleOnGestureListener {

private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;

@Override
public boolean onDown(MotionEvent e) {
return super.onDown(e);
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
result = onSwipeRight();
} else {
result = onSwipeLeft();
}
}
} else {
if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY > 0) {
result = onSwipeBottom();
} else {
result = onSwipeTop();
}
}
}
} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
}

public boolean onSwipeRight() {
return false;
}

public boolean onSwipeLeft() {
return false;
}

public boolean onSwipeTop() {
return false;
}

public boolean onSwipeBottom() {
return false;
}
}

我只需要左右滑动,但由于某种原因,滑动不起作用。是的,它不起作用,这意味着什么也没有发生。我不明白如何纠正这个问题。我知道 SO 成员开发的代码正在运行,因为我正在另一个 Activity 中使用它。

最佳答案

您可以尝试使用 android:clickable="true"使 TextView 可点击。不确定这是否能解决问题,但值得一试。我不确定 android:enabled 会做什么,所以它可能会做同样的事情

关于java - OnTouchListener 没有被解雇,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18447104/

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