33

Tôi có một ScrollView bên trong là EditText được thiết lập để cuộn theo chiều dọc. Nhưng nó không cuộn. Thay vào đó, toàn bộ bố cục cuộn, Bất cứ khi nào tôi cố gắng cuộn EditText. Dưới đây là mã -EditText không cuộn được bên trong ScrollView

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

EditText có id "newTodoText" được đề cập ở đây.

+0

thể trùng lặp của [cuộn editbox bên scrollview] (http://stackoverflow.com/questions/9770252/scrolling-editbox-inside-scrollview) – Elltz

Trả lời

28

Bạn phải chỉ cần thay thế <ScrollView ></ScrollView> của bạn với Custom scrollview này như <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; 
    } 
} 
+0

Điều này đang hoạt động nếu chỉnh sửa văn bản nằm trong chế độ xem cuộn. Cảm ơn .... –

+0

Câu trả lời tuyệt vời – vnshetty

+0

Tuyệt vời ..... Cảm ơn bạn rất nhiều ... – ydnas

0
  • Bạn đã đặt ScrollView làm bố cục bố mẹ nhất. Đó là lý do toàn bộ bố cục của bạn được cuộn.
  • Như xa như EditText của bạn có liên quan, bạn đã sử dụng android:scrollbars = "vertical" có nghĩa là nếu EditText tăng srollBar dọc sẽ xuất hiện. Bạn sẽ nhận được các thanh cuộn và cuộn tác chỉ khi EditText có đủ cao dữ liệu ..

Hope this helps ...

+0

Ngay cả nếu dữ liệu là đủ cao, EditText không thể cuộn được. làm thế nào tôi có thể làm cho nó cuộn? – Naddy

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

Điều này sẽ làm cho toàn bộ bố cục của bạn có thể cuộn được.

Đối với thiết EditText bạn cuộn add android:scrollbars="vertical" để bạn EditText

Trong code của bạn đã nó viết

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

Tháo từ code.It sẽ làm việc tốt

-2

Bạn đã thiết lập " Cuộn Chế độ xem "tới cha mẹ của bạn - điều này có nghĩa là nó cuộn toàn bộ bố cục.

Nếu bạn muốn cuộn "Chỉnh sửa văn bản" cụ thể thì bạn phải đặt một chế độ xem Cuộn khác cho văn bản chỉnh sửa đó.

+2

bạn quên liên kết. – Jakob

73
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; 
    } 
}); 
+9

điều này nên được chấp nhận trả lời, người đàn ông tuyệt vời :) – Antarix

+4

Có thực sự..Mã này làm việc .. Thẳng và đơn giản ... Tuyệt vời Amsheer ... Cảm ơn tuyệt vời – John

+0

Câu trả lời đỉnh cao nhưng một câu hỏi, là nó có thể chỉ đăng ký liên lạc này chặn (di chuyển văn bản chỉnh sửa) khi có đủ nội dung trong văn bản chỉnh sửa để có thể cuộn được? –

11
mEdtText1.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View view, MotionEvent motionEvent) { 
       mScrlMain.requestDisallowInterceptTouchEvent(true); 
       return false; 
      } 
     }); 

// thay đổi nghe mEdtText1 với đối tượng chỉnh sửa văn bản của bạn và cũng có thể thay đổi mScrlMain với đối tượng xem di chuyển của bạn họ làm việc chắc chắn.

+1

Điều này làm việc tuyệt vời cho tôi. Tôi không thể làm Amsheer làm việc. Cảm ơn bạn! – moberme

+0

Đây là câu trả lời đúng. – Shirane85

+0

đơn giản và chính xác hoạt động. cảm ơn :) – iroiroys

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


    }); 
1

bạn nên sử dụng lớp NestedScrollView. Lớp này hỗ trợ con cuộn trong cuộn phụ huynh. Lớp này có thể là một đứa trẻ hoặc một phụ huynh.

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

Một cách đơn giản để làm như vậy được đề cập dưới đây với một số mô tả

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

Sử dụng này Mã của nó làm việc

trong Xml

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

trong Pragraming

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; 
      } 
     }); 
Các vấn đề liên quan