0

我正在尝试制作一个放置在 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/

我现在有点工作,但我有两个问题:

  1. 如何控制滚动以使线条不会被“截断”(请参阅​​上图,屏幕上的第一行有点“截断”)。
  2. 为什么当我以编程方式向上/向下滚动页面时,EditText 是自动全选(请参阅上图,滚动后整个屏幕变为橙色)?
  3. 为什么当我更改 MyGestureDetector 以检测 Y 轴上的甩动(垂直甩动)并以编程方式滚动 EditText 时,它不起作用?即使我也对 ScrollGestureDetector 进行了更改,它也不起作用。它与 ScrollView 的行为有关吗?

谢谢!

4

3 回答 3

1

"3. programmatically scroll the EditText, it doesnt' work"

Post a Runnable with scrolling code to ScrollView instead of calling scrolling methods directly.

post (Runnable action)

Causes the Runnable to be added to the message queue. The runnable will be run on the user interface thread.

from: ScrollView and programmatically scrolling

Lance Nanek

There are past threads on this you can dig up. Basically you have to give the ScrollView a chance to realize it has had stuff added to it. For example:
scroll.post(new Runnable() {
public void run() {
scroll.fullScroll(ScrollView.FOCUS_DOWN); } });

于 2010-11-17T22:39:41.163 回答
0

它迟到了,但可能会帮助一些人解决这个问题。添加最后一个元素并为滚动视图更新它需要大约 200 微秒,所以这肯定会起作用。

void scrollDown()
{
    Thread scrollThread = new Thread(){
        public void run(){
            try {
                sleep(200);
                ChatActivity.this.runOnUiThread(new Runnable() {
                    public void run() {
                        myScrollView.fullScroll(View.FOCUS_DOWN);
                    }    
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    scrollThread.start();
}

只需scrollDown();在将元素添加到滚动视图后调用。

于 2013-08-03T20:38:49.040 回答
0

将以下行添加到 xml edittext 控件,

android:scrollbars="vertical" android:fadeScrollbars="false"

然后将此留置权添加到编码中

"EditText Control Object".setMovementMethod(new ScrollingMovementMethod());
于 2012-07-02T08:57:49.983 回答