How to scroll the edittext inside the scrollview

前端 未结 12 1865
南笙
南笙 2020-12-02 10:27

I have a scrollview inside which there is a editext which is multiline. I want to scroll the edittext to see the lower content but it can\'t be done.

相关标签:
12条回答
  • 2020-12-02 10:52

    Kotlin version

    Add the following lines to the EditText in your xml:

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

    Add this to the Activity/Fragment:

    myEditText.setOnTouchListener { view, event ->
       view.parent.requestDisallowInterceptTouchEvent(true)
       if ((event.action and MotionEvent.ACTION_MASK) == MotionEvent.ACTION_UP) {
           view.parent.requestDisallowInterceptTouchEvent(false)
       }
       return@setOnTouchListener false
    }
    
    0 讨论(0)
  • 2020-12-02 10:52

    First create a custom Scrollview class as given below:

    import android.content.Context;
    import android.util.AttributeSet;
    import android.util.Log;
    import android.view.MotionEvent;
    import android.widget.ScrollView;
    
    public class MyCustomScrollview extends ScrollView {
    
        public VerticalScrollview(Context context) {
            super(context);
        }
    
        public VerticalScrollview(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public VerticalScrollview(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @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;
        }
    }
    
    1. Now in the activity or fragment where you are using the EditText, write the following code for the EditText object that you want to scroll inside the Scrollview :

      import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.widget.EditText;

      public class MainActivity extends AppCompatActivity {

      EditText et;
      VerticalScrollview sv;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
      
          init();
      }
      
      private void init() {
          sv = findViewById(R.id.sv);
          et = findViewById(R.id.et);
          et.setOnTouchListener(new View.OnTouchListener() {
              @Override
              public boolean onTouch(View v, MotionEvent event) {
                  if (v.getId() == R.id.sv) {
                      v.getParent().requestDisallowInterceptTouchEvent(true);
                      switch (event.getAction() & MotionEvent.ACTION_MASK) {
                          case MotionEvent.ACTION_UP:
                              v.getParent().requestDisallowInterceptTouchEvent(false);
                              break;
                      }
                  }
                  return false;
              }
          });
      }
      

      }

    3.The follwing xml is given below:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">
    
        <com.example.ayan.scrollableedittext.VerticalScrollview
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/sv">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="600dp"
                    android:gravity="center">
                    <EditText
                        android:id="@+id/et"
                        android:layout_width="200dp"
                        android:layout_height="80dp"
                        android:nestedScrollingEnabled="true"
                        android:gravity="start" />
                </LinearLayout>
            </LinearLayout>
        </com.example.ayan.scrollableedittext.VerticalScrollview>
    
    </LinearLayout>
    
    0 讨论(0)
  • 2020-12-02 10:53

    Thanks to Hariharan, vovahost I got the same solution, but added performClick() to avoid a warning.

    EditText (XML):

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

    Code:

    edit_text.setOnTouchListener { view, event ->
        view.parent.requestDisallowInterceptTouchEvent(true)
        if ((event.action and MotionEvent.ACTION_MASK) == MotionEvent.ACTION_SCROLL) {
            view.parent.requestDisallowInterceptTouchEvent(false)
        } else {
            performClick()
        }
        return@setOnTouchListener false
    }
    
    0 讨论(0)
  • 2020-12-02 10:58

    I would vote up the updated answer provided by @Ayman Mahgoub, but I do not have a high enough reputation. His code works. I had to change return true to return false. You put the .setOnTouchListener() method in your java class and put the three lines:

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

    in the corresponding xml file. You can see the implementation in the android emulator, and on the android phone you use for testing. Thank you so much @Ayman Mahgoub and @Hariharan!

    0 讨论(0)
  • 2020-12-02 10:58

    Below Answer will help you to make Edit Text scrollable inside Scroll View and also show Count of it.

    1.Make one rectangle_with_border_gray.xml file in @drawable folder.

     <?xml version="1.0" encoding="utf-8"?>
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <corners android:radius="0dp"/>
            <solid android:color="#FFFFFF"/>
            <stroke android:color="#7f848686"
                android:width="0.01dp"/>
        </shape>
    

    2.Then, In @layout write below code in scroll view.

    <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="none">
    
                 <RelativeLayout
                    android:id="@+id/sv_profile_creation_layout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#FFFFFF">
    
                        <LinearLayout
                            android:id="@+id/LL_optional_message"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/tv_optional_message_title"
                            android:background="@drawable/rectangle_with_border_gray"
                            android:orientation="horizontal"
                            android:padding="@dimen/margin_02_dp">
    
                            <EditText
                                android:id="@+id/et_optional_message"
                                android:layout_width="match_parent"
                                android:layout_height="100dp"
                                android:background="@color/colorWhite"
                                android:gravity="start"
                                android:hint="@string/why_not_leave_a_message"
                                android:inputType="textMultiLine"
                                android:isScrollContainer="true"
                                android:maxLines="5"
                                android:overScrollMode="always"
                                android:padding="8dp"
                                android:scrollbarStyle="insideInset"
                                android:scrollbars="vertical"
                                android:textColor="@color/colorEditTextHintNormal"
                                android:textColorHint="@color/colorEditTextHint"
                                android:textSize="@dimen/margin_14_dp" />
    
                        </LinearLayout>
    
                        <TextView
                            android:id="@+id/tv_description_count"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentRight="true"
                            android:layout_below="@+id/LL_optional_message"
                            android:layout_centerVertical="true"
                            android:layout_marginBottom="@dimen/margin_16_dp"
                            android:ellipsize="end"
                            android:gravity="center"
                            android:maxLines="1"
                            android:text="0/200"
                            android:textColor="@color/colorLittleDarkGray"
                            android:textSize="@dimen/margin_12_dp"
                            android:textStyle="normal" />
    
             </RelativeLayout>
     </ScrollView>
    

    3.Then, Inside your Activity or Fragment write below code:

    TextView tv_description_count = (TextView) view.findViewById(R.id.tv_description_count);
    
    EditText et_optional_message = (EditText) findViewById(R.id.et_optional_message);
    
    private void makeScrollable(){
                et_optional_message.addTextChangedListener(mTextEditorWatcher);
                et_optional_message.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View view, MotionEvent motionEvent) {
                        view.getParent().requestDisallowInterceptTouchEvent(true);
                        switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
                            case MotionEvent.ACTION_SCROLL:
                                view.getParent().requestDisallowInterceptTouchEvent(false);
                                return true;
                            case MotionEvent.ACTION_BUTTON_PRESS:
                                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                                imm.showSoftInput(et_optional_message, InputMethodManager.SHOW_IMPLICIT);
                        }
                        return false;
                    }
                });
            }
    
     private final TextWatcher mTextEditorWatcher = new TextWatcher() {
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }
    
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    //This sets a textview to the current length
                    tv_description_count.setText(String.valueOf(s.length()));
                }
    
                public void afterTextChanged(Editable s) {
                }
            };
    

    Happy Coding......

    0 讨论(0)
  • 2020-12-02 11:03

    First add this at XML

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

    Then add the same above "OnTouch" but make it return "false" not "true"

    public boolean onTouch(View view, MotionEvent event) {
    
                        if (view.getId() == R.id.DwEdit) {
                            view.getParent().requestDisallowInterceptTouchEvent(true);
                            switch (event.getAction()&MotionEvent.ACTION_MASK){
                            case MotionEvent.ACTION_UP:
                                view.getParent().requestDisallowInterceptTouchEvent(false);
                                break;
                            }
                        }
                        return false;
    }
    
    0 讨论(0)
提交回复
热议问题