我正在尝试检测“向左滑动”、“向右滑动”和“向下滑动”。
请看下面的代码
段落阅读器.java
package k.k;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import android.os.Bundle;
import android.app.Activity;
import android.speech.tts.TextToSpeech;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class ParagraphReader extends Activity implements TextToSpeech.OnInitListener {
private TextView paraText;
private TextView paraListQuestionIndicator;
private ImageView speaker;
private DatabaseConnector database;
private List<String>paraList;
private int currentQuestion;
private TextToSpeech tts;
@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);
paraListQuestionIndicator = (TextView)findViewById(R.id.paralist_question_indicator);
speaker = (ImageView)findViewById(R.id.speak);
speaker.setOnClickListener(new SpeakOutTheText());
paraList = new ArrayList<String>();
database = DatabaseHandler.getInstance(this);
tts = new TextToSpeech(this,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));
setParaListQuestionIndicator(currentQuestion);
}
@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();
currentQuestion--;
if(currentQuestion<0)
{
currentQuestion = paraList.size()-1;
}
paraText.setText(paraList.get(currentQuestion));
setParaListQuestionIndicator(currentQuestion);
return true;
}
public boolean onSwipeLeft()
{
//Toast.makeText(ParagraphReader.this, "Left: "+paraList.size(), Toast.LENGTH_SHORT).show();
currentQuestion++;
if(currentQuestion>paraList.size()-1)
{
currentQuestion = 0;
}
paraText.setText(paraList.get(currentQuestion));
setParaListQuestionIndicator(currentQuestion);
return true;
}
public boolean onSwipeBottom()
{
return true;
}
};
private class SpeakOutTheText implements OnClickListener
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
speak(paraText.getText().toString());
}
}
private void setParaListQuestionIndicator(int number)
{
paraListQuestionIndicator.setText((number+1)+"/"+paraList.size());
}
@Override
public void onInit(int status)
{
// TODO Auto-generated method stub
if(status==TextToSpeech.SUCCESS)
{
int result = tts.setLanguage(Locale.UK);
if(result==TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED)
{
Toast.makeText(this, "Language Not Supported", Toast.LENGTH_LONG).show();
}
}
}
@Override
public void onDestroy()
{
if(tts!=null)
{
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
public void speak(String text)
{
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
}
段落阅读器.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:minHeight="200dp"
android:ems="30"
android:singleLine="false"
android:clickable="true"
>
</TextView>
</ScrollView>
<TextView
android:id="@+id/paralist_question_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="@+id/speak"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:src="@drawable/speaker2" />
</RelativeLayout>
还有手势检测代码,
OnSwipeTouchListener.java
这段代码来自这里
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;
}
}
此代码在Virtual Device中运行良好,但令我惊讶的是,它在真实设备上不起作用!我有 Micromax Canvas2,我用我的手机和 HP 手机进行了测试,这两个都不起作用。
不工作意味着,我认为它不能正确识别任何滑动。“有时”它确实向左和向右滑动,但那是“有时”,为此我也不得不非常努力地拖动它 3-4 次。
这里有什么问题?这不起作用,因为我将侦听器实现为TextView
?
PS:我在这里发布了完整的相关课程,因为上次删除代码无法获得合适的答案。