上滚动型滚动编程的EditText [英] Scrolling EditText on ScrollView programmatically

查看:206
本文介绍了上滚动型滚动编程的EditText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让被放置在一个滚动型和滚动编程控制(当检测到左/右甩时),不可编辑的EditText。

I am trying to make a non-editable EditText that is placed in a ScrollView and scrolling is controlled programmatically (when a left/right fling is detected).

好吧,这里是我的简单的布局:

Ok, here's my simple layout:

    <?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>

和这里是我的简单的程序:

And here is my simple program:

    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和滚动型两个自定义simplegesture类。对于EditText上,我试图检测到左/右一扔和检测时,我滚动起来1页向上/向下。附着在滚动型自定义simplegesture是禁用的手指滚动。

So, to explain it simply, I have two custom simplegesture classes that I attached to the EditText and the ScrollView. For the EditText, I'm trying to detect a left/right fling and when detected, I'm scrolling the it up 1 page up/down. The custom simplegesture attached to the ScrollView is to disable finger scrolling.

下面是一个截屏右侧一扔做后:
http://img830.imageshack.us/i/textcut.png/

Here's a screen shot after a right fling was done: http://img830.imageshack.us/i/textcut.png/

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

I kinda works right now but I have two questions:


  1. 如何控制滚动,使线不会得到切客(请参阅​​上文的地方在屏幕上的第一行是有点CUF客的图片)。

  2. 为什么当我滚动页面向上/向下编程时,是的EditText自动全选(请参考上面的图片滚动的地方后,整个屏幕变为橙色)?

  3. 为什么当我改变MyGestureDetector检测一扔在Y轴(垂直一扔)和编程方式滚动的EditText,它不'的工作?即使我取得了ScrollGestureDetector变化太大这是行不通的。它有什么做的滚动型的行为?

感谢您!

推荐答案

3.编程滚动的EditText,它不工作

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

发表一个Runnable带滚动code,以滚动型的,而不是直接调用滚动的方法。

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

后(Runnable的动作)

post (Runnable action)

导致要添加到消息队列中的可运行。可运行将在运行
  用户界面线程。

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

从:<一href=\"http://groups.google.com/group/android-developers/browse_thread/thread/f11730d8395b3001/bc911db0729ccf5d?show_docid=bc911db0729ccf5d\"相对=nofollow>滚动型和编程滚动

兰斯Nanek

有过去在这你可以线程
  挖出。基本上,你必须给
  滚动型机会来实现它
  有东西添加进去。例如:结果
  scroll.post(新的Runnable(){结果
  公共无效的run(){结果
        scroll.fullScroll(ScrollView.FOCUS_DOWN);
  }});

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); } });

这篇关于上滚动型滚动编程的EditText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆