EditText 在 ScrollView 内不可滚动 [英] EditText not scrollable inside ScrollView

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

问题描述

我有一个 ScrollView,里面有一个 EditText,它被设置为垂直滚动.但它不滚动.而是整个布局滚动,每当我尝试滚动 EditText.下面是代码-

I have a ScrollView inside which is an EditText which is set to scroll vertically. But it does not scrolls. Instead the whole layout scrolls, Whenever i try to scroll the EditText. Below is the code -

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TextView
    android:id="@+id/b1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="39dp"
    android:text="Title"
    android:textColor="#3bb9ff"
    android:textSize="15sp" />

<EditText
    android:id="@+id/Text1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="Title"
    android:singleLine="true" >

    <requestFocus />

</EditText>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Content"
    android:layout_marginTop="50dp"
    android:textColor="#3bb9ff"
    android:textSize="15sp"
   />


<EditText
    android:id="@+id/newTodoText"
    android:layout_width="fill_parent"
    android:layout_height="match_parent" 
    android:minLines="2"
    android:maxLines="7"
     android:hint="Write something"
     android:scrollbars = "vertical" >
</EditText>
<Button
    android:id="@+id/Add"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="Add" />


</LinearLayout>
</ScrollView>

ID 为newTodoText"的 EditText 在这里有问题.

The EditText with id "newTodoText" is in question here.

推荐答案

您只需将 <ScrollView ></ScrollView> 替换为这个自定义 ScrollView 就像 <com.example.VerticalScrollview ></com.example.VerticalScrollview >

You have to just replace your <ScrollView ></ScrollView> with this Custom ScrollView like <com.example.VerticalScrollview > </com.example.VerticalScrollview >

package com.example;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ScrollView;

public class VerticalScrollview extends ScrollView{

    public VerticalScrollview(Context context) {
        super(context);
    }

     public VerticalScrollview(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        public VerticalScrollview(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        final int action = ev.getAction();
        switch (action)
        {
            case MotionEvent.ACTION_DOWN:
                    Log.i("VerticalScrollview", "onInterceptTouchEvent: DOWN super false" );
                    super.onTouchEvent(ev);
                    break;

            case MotionEvent.ACTION_MOVE:
                    return false; // redirect MotionEvents to ourself

            case MotionEvent.ACTION_CANCEL:
                    Log.i("VerticalScrollview", "onInterceptTouchEvent: CANCEL super false" );
                    super.onTouchEvent(ev);
                    break;

            case MotionEvent.ACTION_UP:
                    Log.i("VerticalScrollview", "onInterceptTouchEvent: UP super false" );
                    return false;

            default: Log.i("VerticalScrollview", "onInterceptTouchEvent: " + action ); break;
        }

        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        super.onTouchEvent(ev);
        Log.i("VerticalScrollview", "onTouchEvent. action: " + ev.getAction() );
         return true;
    }
}

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

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