我正在尝试制作一个放置在 ScrollView 中的不可编辑的 EditText,并以编程方式控制滚动(当检测到左/右翻转时)。
好的,这是我的简单布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ScrollView
android:id="@+id/sv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText android:id="@+id/maintext"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:longClickable="false"
android:selectAllOnFocus="false"
android:focusable="false"
android:editable="false"/>
</ScrollView>
</LinearLayout>
这是我的简单程序:
package com.test.testscroll;
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.Toast;
public class TestScroll extends Activity {
private EditText mMainText;
private ScrollView mScrollView;
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;
private GestureDetector scrollGestureDetector;
View.OnTouchListener scrollGestureListener;
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private final String testString = "This is a very long line for testing purpose. Line number 1" +
"This is a very long line for testing purpose. Line number 2" +
"This is a very long line for testing purpose. Line number 3" +
"This is a very long line for testing purpose. Line number 4" +
"This is a very long line for testing purpose. Line number 5" +
"This is a very long line for testing purpose. Line number 6" +
"This is a very long line for testing purpose. Line number 7" +
"This is a very long line for testing purpose. Line number 8" +
"This is a very long line for testing purpose. Line number 9" +
"This is a very long line for testing purpose. Line number 10" +
"This is a very long line for testing purpose. Line number 11" +
"This is a very long line for testing purpose. Line number 12" +
"This is a very long line for testing purpose. Line number 13" +
"This is a very long line for testing purpose. Line number 14" +
"This is a very long line for testing purpose. Line number 15" +
"This is a very long line for testing purpose. Line number 16" +
"This is a very long line for testing purpose. Line number 17" +
"This is a very long line for testing purpose. Line number 18" +
"This is a very long line for testing purpose. Line number 19" +
"This is a very long line for testing purpose. Line number 20" +
"This is a very long line for testing purpose. Line number 21" +
"This is a very long line for testing purpose. Line number 22" +
"This is a very long line for testing purpose. Line number 23" +
"This is a very long line for testing purpose. Line number 24" +
"This is a very long line for testing purpose. Line number 25" +
"This is a very long line for testing purpose. Line number 26" +
"This is a very long line for testing purpose. Line number 27" +
"This is a very long line for testing purpose. Line number 28" +
"This is a very long line for testing purpose. Line number 29" +
"This is a very long line for testing purpose. Line number 30" +
"This is a very long line for testing purpose. Line number 31" +
"This is a very long line for testing purpose. Line number 32" +
"This is a very long line for testing purpose. Line number 33" +
"This is a very long line for testing purpose. Line number 34" +
"This is a very long line for testing purpose. Line number 35" +
"This is a very long line for testing purpose. Line number 36" +
"This is a very long line for testing purpose. Line number 37" +
"This is a very long line for testing purpose. Line number 38" +
"This is a very long line for testing purpose. Line number 39" +
"This is a very long line for testing purpose. Line number 40" +
"This is a very long line for testing purpose. Line number 41" +
"This is a very long line for testing purpose. Line number 42" +
"This is a very long line for testing purpose. Line number 43" +
"This is a very long line for testing purpose. Line number 44" +
"This is a very long line for testing purpose. Line number 45" +
"This is a very long line for testing purpose. Line number 46" +
"This is a very long line for testing purpose. Line number 47" +
"This is a very long line for testing purpose. Line number 48" +
"This is a very long line for testing purpose. Line number 49" +
"This is a very long line for testing purpose. Line number 50";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mMainText = (EditText) findViewById(R.id.maintext);
mScrollView = (ScrollView) findViewById(R.id.sv);
mMainText.setText(testString);
// Gesture detection
gestureDetector = new GestureDetector(new MyGestureDetector());
gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return true;
}
return false;
}
};
mMainText.setOnTouchListener(gestureListener);
scrollGestureDetector = new GestureDetector(new ScrollGestureDetector());
scrollGestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (scrollGestureDetector.onTouchEvent(event)) {
return true;
}
return false;
}
};
mScrollView.setOnTouchListener(scrollGestureListener);
}
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// right to left swipe
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(TestScroll.this, "Left Swipe", Toast.LENGTH_SHORT).show();
mScrollView.pageScroll(ScrollView.FOCUS_UP);
return true;
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(TestScroll.this, "Right Swipe", Toast.LENGTH_SHORT).show();
mScrollView.pageScroll(ScrollView.FOCUS_DOWN);
return true;
}
} catch (Exception e) {
// nothing
}
return false;
}
}
class ScrollGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return true;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
{
return true;
}
@Override
public void onLongPress(MotionEvent e)
{
// Do nothing
}
}
}
因此,为了简单地解释它,我有两个自定义的简单手势类,它们附加到 EditText 和 ScrollView。对于 EditText,我正在尝试检测左/右翻转,当检测到时,我将其向上/向下滚动 1 页。附加到 ScrollView 的自定义简单手势是禁用手指滚动。
这是完成正确投掷后的屏幕截图:http: //img830.imageshack.us/i/textcut.png/
我现在有点工作,但我有两个问题:
- 如何控制滚动以使线条不会被“截断”(请参阅上图,屏幕上的第一行有点“截断”)。
- 为什么当我以编程方式向上/向下滚动页面时,EditText 是自动全选(请参阅上图,滚动后整个屏幕变为橙色)?
- 为什么当我更改 MyGestureDetector 以检测 Y 轴上的甩动(垂直甩动)并以编程方式滚动 EditText 时,它不起作用?即使我也对 ScrollGestureDetector 进行了更改,它也不起作用。它与 ScrollView 的行为有关吗?
谢谢!