如何滚动scrollview中的edittext

时间:2014-06-26 11:04:20

标签: android user-interface scrollview

我有一个滚动视图,其中有一个多行的editext。我想滚动编辑文本以查看较低的内容,但无法完成。

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

enter image description here

你们可以帮我吗?我认为当光标位于其中时,editText正在获得焦点。

谢谢.. !!!!!

11 个答案:

答案 0 :(得分:63)

试试这个..

将以下行添加到EditText

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

示例

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

修改

编程

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

答案 1 :(得分:58)

首先在XML

添加它
android:scrollbarStyle="insideInset"
android:scrollbars="vertical" 
android:overScrollMode="always"

然后添加上面的#34; OnTouch&#34;但要让它回归&#34; false&#34;不是&#34;是&#34;

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

答案 2 :(得分:8)

您应该使用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>           

答案 3 :(得分:3)

以@Ayman Mahgoub和@Hariharan的答案为基础。该解决方案工作得很好,除了在滚动编辑文本时没有滚动动量。一旦手指抬起,滚动立即停止。

获取滚动动量包围EditText周围的滚动视图,并让EditText的高度换行内容(如果你愿意,可以设置minHeight)。从编辑文本中删除以下行:

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

现在,围绕EditText的嵌套ScrollView负责滚动。更新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; 

请注意,此解决方案现在与视图层次结构紧密耦合。

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

答案 4 :(得分:3)

使用此代码

以XML格式

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

编程

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

答案 5 :(得分:1)

下面的答案将帮助您在滚动视图中滚动编辑文本,并显示它的计数。

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.然后,在@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.然后,在您的活动或片段内写下代码:

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

快乐编码......

答案 6 :(得分:1)

Kotlin版

将以下行添加到xml中的EditText:

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

将此添加到活动/片段:

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
}

答案 7 :(得分:0)

这将滚动EditText并使其可编辑:

首先在XML文件中添加:

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

然后在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;
       }
});

答案 8 :(得分:0)

首先创建一个自定义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. 现在,在使用EditText的活动或片段中,为要在Scrollview中滚动的EditText对象编写以下代码:

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

    公共类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;
            }
        });
    }
    

    }

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

答案 9 :(得分:0)

感谢Hariharanvovahost,我得到了相同的解决方案,但是添加了performClick()以避免警告。

EditText(XML):

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

代码:

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
}

答案 10 :(得分:-1)

在主要活动中添加此方法:

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