简体   繁体   English

如何在滚动视图中滚动编辑文本

[英]How to scroll the edittext inside the scrollview

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.我想滚动 edittext 以查看较低的内容,但无法完成。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:background="@android:color/holo_blue_light"
        android:gravity="center" >
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="View Complaint"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </LinearLayout>
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:padding="20dp" >
            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:text="Order Number 0100C1"
                android:textAppearance="?android:attr/textAppearanceMedium" />
            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:text="Name of ClientClient 1"
                android:textAppearance="?android:attr/textAppearanceMedium" />
            <TextView
                android:id="@+id/textView4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:text="Subject : Measurement Issues"
                android:textAppearance="?android:attr/textAppearanceMedium" />
            <TextView
                android:id="@+id/textView5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="25dp"
                android:text="Description"
                android:textAppearance="?android:attr/textAppearanceMedium" />
            <TextView
                android:id="@+id/textView6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:text="Lorem ipsum dolor sit amet, sapien etiam, nunc amet dolor ac odio mauris justo. Luctus arcu, urna praesent at id quisque ac. Arcu massa vestibulum malesuada, integer vivamus el/ eu "
                android:textAppearance="?android:attr/textAppearanceMedium" />
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >
                <TextView
                    android:id="@+id/textView7"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="2dp"
                    android:text="Assign to"
                    android:textAppearance="?android:attr/textAppearanceMedium" />
                <Spinner
                    android:id="@+id/spinner1"
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:entries="@array/array_name" />
            </LinearLayout>
            <EditText
                android:id="@+id/editText1"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:layout_marginTop="15dp"
                android:background="#eeeeee"
                android:inputType="textMultiLine"
                android:singleLine="false"
                android:text="Android applications normally run entirely on a single thread by              default the “UI thread” or the “main thread”.
        
            android:textAppearance="?android:attr/textAppearanceMedium" ></EditText>
            <TextView
                android:id="@+id/textView5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:text="Comment History"
                android:textAppearance="?android:attr/textAppearanceMedium" />
            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="fill_parent"
                android:layout_height="147dp"
                android:src="@drawable/adddd" />
            <CheckBox
                android:id="@+id/checkBox1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Close Complaints"
                android:textAppearance="?android:attr/textAppearanceLarge" />
            <Button
                android:id="@+id/login"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="match_parent"
                android:layout_height="45dp"
                android:layout_below="@+id/ll"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="15dp"
                android:background="@drawable/login_btn"
                android:text="Submit"
                android:textColor="@android:color/holo_blue_dark"
                android:textSize="25dp"
                android:textStyle="bold" />
        </LinearLayout>
    </ScrollView>
</LinearLayout>

在此处输入图像描述

Can you guys help me in that.你们能帮我吗? I think editText is getting the focus when cursor is inside it.我认为当 cursor 在其中时,editText 正在获得焦点。

Thanks..!!!!!谢谢..!!!!!

Try this..尝试这个..

Add below lines into your EditText将以下几行添加到您的EditText

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

Example例子

   <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_marginTop="15dp"
        android:background="#eeeeee"
        android:inputType="textMultiLine"
        android:singleLine="false"
        android:overScrollMode="always"
        android:scrollbarStyle="insideInset"
        android:scrollbars="vertical"
        android:text="Android applications normally run entirely on a single thread by              default the “UI thread” or the “main thread”.
        android:textAppearance="?android:attr/textAppearanceMedium" >
    </EditText>

EDIT编辑

Programmatically以编程方式

youredittext.setOnTouchListener(new OnTouchListener() {

      public boolean onTouch(View v, MotionEvent event) {
            if (youredittext.hasFocus()) {
               v.getParent().requestDisallowInterceptTouchEvent(true);
               switch (event.getAction() & MotionEvent.ACTION_MASK){
               case MotionEvent.ACTION_SCROLL:
                      v.getParent().requestDisallowInterceptTouchEvent(false);
                      return true;
                }
             }
             return false;
       }
});

First add this at XML首先在 XML 添加这个

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

Then add the same above "OnTouch" but make it return "false" not "true"然后在“OnTouch”上方添加相同的内容,但使其返回“false”而不是“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;
}

Kotlin version科特林版

Add the following lines to the EditText in your xml:将以下行添加到 xml 中的 EditText:

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
}

you should use NestedScrollView class.你应该使用 NestedScrollView 类。 This class support child scrolling inside parent scrolling.此类支持父滚动内的子滚动。 This class can be a child or a parent.这个班级可以是孩子或家长。

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

Use this code it's working使用此代码它正在工作

In XML在 XML 中

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

In programming在编程中

edittext.setOnTouchListener(new View.OnTouchListener() {

    public boolean onTouch(View view, MotionEvent event) {

        if (view.getId() == R.id.edittext) {
            view.getParent().requestDisallowInterceptTouchEvent(true);
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_UP:
                view.getParent().requestDisallowInterceptTouchEvent(false);
                break;
            }
        }
        return false;
    }
});

Building upon the answer by @Ayman Mahgoub and @Hariharan.基于@Ayman Mahgoub 和@Hariharan 的回答。 The solution worked great except while scrolling the edit text there is no scroll momentum.该解决方案效果很好,除了在滚动编辑文本时没有滚动动力。 As soon as the finger lifts up, the scrolling immediately stops.手指一抬起,滚动立即停止。

To gain scroll momentum wrap a scrollview around the EditText and let the EditText's height wrap content (set minHeight if you'd like).为了获得滚动动力,在 EditText 周围包裹一个滚动视图,让 EditText 的高度包裹内容(如果你愿意,可以设置 minHeight)。 Remove the following lines from the edit text:从编辑文本中删除以下行:

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

Now the nested ScrollView wrapped around the EditText takes care of the scrolling.现在包裹在 EditText 周围的嵌套 ScrollView 负责滚动。 Update the onTouch handler to take into account the updated view hierarchy:更新 onTouch 处理程序以考虑更新的视图层次结构:

public boolean onTouch(View view, MotionEvent event) {
    v.getParent().getParent().requestDisallowInterceptTouchEvent(true);
    switch (event.getAction() & MotionEvent.ACTION_MASK){
        case MotionEvent.ACTION_UP:
            v.getParent().getParent().requestDisallowInterceptTouchEvent(false);
            break;
    }
    return false; 

Be warned, this solution is now tightly coupled to the view hierarchy.请注意,此解决方案现在与视图层次结构紧密耦合。

Gist: https://gist.github.com/atoennis/7549fba2634d0abccddf要点: https : //gist.github.com/atoennis/7549fba2634d0abccddf

I would vote up the updated answer provided by @Ayman Mahgoub, but I do not have a high enough reputation.我会投票支持@Ayman Mahgoub 提供的更新答案,但我的声誉不够高。 His code works.他的代码有效。 I had to change return true to return false.我不得不改变 return true 返回 false。 You put the .setOnTouchListener() method in your java class and put the three lines:您将 .setOnTouchListener() 方法放在您的 java 类中并放置三行:

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

in the corresponding xml file.在相应的 xml 文件中。 You can see the implementation in the android emulator, and on the android phone you use for testing.您可以在 android 模拟器和用于测试的 android 手机上查看实现。 Thank you so much @Ayman Mahgoub and @Hariharan!非常感谢@Ayman Mahgoub 和@Hariharan!

This will scroll the EditText and make it editable:这将滚动EditText并使其可编辑:

First in the XML file, add this:首先在 XML 文件中,添加以下内容:

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

Then in the Java file, add this:然后在 Java 文件中添加:

EditText.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(EditText, InputMethodManager.SHOW_IMPLICIT);
           }
           return false;
       }
});

The simplest way is by doing Extention in Kotlin:最简单的方法是在 Kotlin 中执行扩展:

First add this extension:首先添加这个扩展:

fun EditText.enableScrollText()
{
    overScrollMode = View.OVER_SCROLL_ALWAYS
    scrollBarStyle = View.SCROLLBARS_INSIDE_INSET
    isVerticalScrollBarEnabled = true
    setOnTouchListener { view, event ->
        if (view is EditText) {
            if(!view.text.isNullOrEmpty()) {
                view.parent.requestDisallowInterceptTouchEvent(true)
                when (event.action and MotionEvent.ACTION_MASK) {
                    MotionEvent.ACTION_UP -> view.parent.requestDisallowInterceptTouchEvent(false)
                }
            }
        }
        false
    }
}

and finally easily call this for your edittext:最后轻松地为您的编辑文本调用它:

editText.enableScrollText();

This extention works well in any case like inside ScrollView or RecyclerView or BottomSheet此扩展在任何情况下都适用,例如在ScrollViewRecyclerViewBottomSheet

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. 1.在@drawable 文件夹中制作一个 rectangle_with_border_gray.xml 文件。

 <?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. 2.然后,在@layout 中在滚动视图中编写下面的代码。

<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: 3.然后,在您的 Activity 或 Fragment 中写入以下代码:

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......快乐编码......

First create a custom Scrollview class as given below:首先创建一个自定义 Scrollview 类,如下所示:

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 :现在,在您使用 EditText 的活动或片段中,为要在 Scrollview 内滚动的 EditText 对象编写以下代码:

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

    public class MainActivity extends AppCompatActivity {公共类 MainActivity 扩展 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: 3.下面的xml如下:

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

Thanks to Hariharan , vovahost I got the same solution, but added performClick() to avoid a warning.感谢Hariharanvovahost我得到了相同的解决方案,但添加了performClick()以避免警告。

EditText (XML):编辑文本(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
}

Add this method in main activity:在主要活动中添加此方法:

It worked for me in Fragment它在片段中对我有用

 @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (v instanceof EditText) {
                Rect outRect = new Rect();
                v.getGlobalVisibleRect(outRect);
                if (!outRect.contains((int) event.getRawX(), (int) event.getRawY())) {
                    v.clearFocus();
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
            }
        }
        return super.dispatchTouchEvent(event);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM