【问题标题】:EditText not scrollable inside ScrollViewEditText 在 ScrollView 内不可滚动
【发布时间】:2013-05-12 09:53:06
【问题描述】:

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

<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 在这里存在问题。

【问题讨论】:

标签: android android-layout android-edittext android-view android-scrollview


【解决方案1】:
EditText EtOne = (EditText) findViewById(R.id.EditText01);
EtOne.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (v.getId() == R.id.EditText01) {
            v.getParent().requestDisallowInterceptTouchEvent(true);
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_UP:
                v.getParent().requestDisallowInterceptTouchEvent(false);
                break;
            }
        }
        return false;
    }
});

【讨论】:

  • 是的,真的..此代码有效..简单明了...真棒 Amsheer...非常感谢
  • 一流的答案,但有一个问题,当编辑文本中有足够的内容可以滚动时,是否可以仅注册此触摸拦截(滚动编辑文本)?
  • @Jack.Ramsden 在这种情况下,您会将整个内容包装在 onTouch 中,除了 if 条件中的 return 语句,它会获取大于一定数量的字符的 EditText 长度结束检查(例如 50 个!)。
  • 是否有必要删除 ACTION_UP 上的禁止拦截?我认为这会在手势结束时自然重置。
【解决方案2】:

您只需将 &lt;ScrollView &gt;&lt;/ScrollView&gt; 替换为此自定义 ScrollView,例如 &lt;com.example.VerticalScrollview &gt; &lt;/com.example.VerticalScrollview &gt;

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

【讨论】:

  • 我尝试过片段视图..它不起作用..有什么建议吗?
  • 对不起...但是这个答案花了我一个小时正确的答案是:stackoverflow.com/questions/24428808/… 寻找:Ayman Mahgoub 答案
  • 正如@Shubh 所评论的,这不适用于片段,这也不适用于图像视图和按钮。这仅适用于 textview。
【解决方案3】:
mEdtText1.setOnTouchListener(new View.OnTouchListener() { 
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                mScrlMain.requestDisallowInterceptTouchEvent(true);
                return false;
            }
        });

// 使用您的编辑文本对象更改听到 mEdtText1 并使用您的滚动视图对象更改 mScrlMain,它们确实有效。

【讨论】:

  • 这对我很有用。我无法让 Amsheer 工作。谢谢!
  • 我喜欢这种干净利落的方式。
【解决方案4】:

你应该使用 NestedScrollView 类。此类支持父滚动中的子滚动。这个类可以是子类或父类。

<android.support.v4.widget.NestedScrollView         
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#d6d8d9">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:maxLines="512"
                android:text=" your content"/>
        <android.support.v4.widget.NestedScrollView
            android:layout_below="@id/ll_button"
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:background="#d6d8d9">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content" 
                android:text="your content"
                android:maxLines="512"/>    
        </android.support.v4.widget.NestedScrollView>       
    </LinearLayout>

</android.support.v4.widget.NestedScrollView> 

【讨论】:

  • 其他答案均无效,但您的答案有效。谢谢。
【解决方案5】:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

这将使您的整个布局可滚动。

要设置可滚动的edittext,请将android:scrollbars="vertical" 添加到您的edittext

在你的代码中已经写好了

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

从代码中删除。它会正常工作

【讨论】:

    【解决方案6】:

    使用此代码的工作原理

    在 XML 中

    android:singleLine="false"
    android:overScrollMode="always"
    android:scrollbarStyle="insideInset"
    android:scrollbars="vertical"
    

    在实践中

    et_feedback.setOnTouchListener(new View.OnTouchListener() {
    
                public boolean onTouch(View view, MotionEvent event) {
    
                    if (view.getId() == R.id.et_feedback) {
                        view.getParent().requestDisallowInterceptTouchEvent(true);
                        switch (event.getAction() & MotionEvent.ACTION_MASK) {
                            case MotionEvent.ACTION_UP:
                                view.getParent().requestDisallowInterceptTouchEvent(false);
                                break;
                        }
                    }
                    return false;
                }
            });
    

    【讨论】:

      【解决方案7】:
      noteEdit.setMovementMethod(new ScrollingMovementMethod());
          ScrollingMovementMethod.getInstance();
      
      
          noteEdit.setOnTouchListener(new View.OnTouchListener() {
      
              @Override
              public boolean onTouch(View v, MotionEvent event) {
      
                  noteEdit.getParent().requestDisallowInterceptTouchEvent(true);
      
                  return false;
              }
      
      
          });
      

      【讨论】:

      • 我认为你写的东西有问题。 ScrollingMovementMethod.getInstance() 未使用...
      【解决方案8】:

      下面提到了一种简单的方法,并附有一些说明

                  myEditText.setOnTouchListener(new View.OnTouchListener() {  //Register a callback to be invoked when a touch event is sent to this view.
                  @Override
                  public boolean onTouch(View v, MotionEvent event) {     
      //Called when a touch event is dispatched to a view. This allows listeners to get a chance to respond before the target view.
                      mScrollView.requestDisallowInterceptTouchEvent(true);
                      return false;
                  }
              });
      

      【讨论】:

        【解决方案9】:
        • 您已将ScrollView 设置为您的父级大多数布局。这就是原因 你的整个布局都会滚动。
        • 就您的 EditText 而言,您使用过 android:scrollbars = "vertical" 这意味着 如果 EditText 增长会出现一个垂直的srollBar。您将获得滚动条和滚动效果 仅当 edittext 的数据足够高时..

        希望这会有所帮助...

        【讨论】:

        • 即使数据足够高,EditText也无法滚动。我怎样才能使它可滚动?
        【解决方案10】:

        XML 代码

            android:gravity="top"
            android:inputType="text|textMultiLine"
            android:lines="5"
        

        使用下面的 Kotlin 代码

            editText.setOnTouchListener { v, event ->
                if (v.id == R.id.editText) {
                    v.parent.requestDisallowInterceptTouchEvent(true)
                    when (event.action and MotionEvent.ACTION_MASK) {
                        MotionEvent.ACTION_UP -> v.parent.requestDisallowInterceptTouchEvent(false)
                    }
                }
                false
            }
        

        【讨论】:

          【解决方案11】:

          我在滑动关闭的底部工作表中遇到了类似的问题,但我怀疑这通常会起作用:

          @SuppressLint("ClickableViewAccessibility")
          fun View.blockAllAncestorsInterceptingTouch() {
              setOnTouchListener { _, _ ->
                  parent?.requestDisallowInterceptTouchEvent(true)
                  false
              }
          }
          

          suppress Lint 是合法的,如果一直返回 false 就不需要调用 performClick 了。

          【讨论】:

            【解决方案12】:

            您已将“滚动视图”设置为您的父级 - 这意味着它会滚动整个布局。

            如果要滚动特定的“编辑文本”,则必须将另一个滚动视图放在该编辑文本上。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2019-10-17
              • 1970-01-01
              • 1970-01-01
              • 2013-09-19
              • 1970-01-01
              • 1970-01-01
              • 2020-06-04
              相关资源
              最近更新 更多