如何在活动中创建带滚动视图的多行edittext?

时间:2014-02-14 11:02:50

标签: android scrollview android-edittext

我想要edittext有3行,如果edittext内容超过3行限制,那么用户应该可以看到垂直滚动条。

7 个答案:

答案 0 :(得分:15)

可能100%工作

<EditText
    android:inputType="textMultiLine"
    android:lines="int number" <!-- Total Lines prior display -->
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:scrollbars="vertical"/>

答案 1 :(得分:11)

使用inputType="textMultiLine"并将android:lines="3"设置为:

<EditText
android:inputType="textMultiLine"
android:lines="3" <!-- Total Lines prior display -->
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:scrollbars="vertical"
/>

答案 2 :(得分:3)

在编辑文本时添加

    android:inputType="textMultiLine" 
    android:maxLines="3">

答案 3 :(得分:1)

我认为这个问题更多的是在scrollview中添加Multiline editText。

在scrollview内部,如果您的EditText有更多文本,您将无法在内部滚动,而是整个页面将滚动。

您可以借助TouchListener来实现这一目标。

edtDescription = (EditText)findViewById(R.id.edt_description);

    edtDescription.setVerticalScrollBarEnabled(true);
    edtDescription.setOverScrollMode(View.OVER_SCROLL_ALWAYS);
    edtDescription.setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET);
    edtDescription.setMovementMethod(ScrollingMovementMethod.getInstance());

    edtDescription.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {

            view.getParent().requestDisallowInterceptTouchEvent(true);
            if ((motionEvent.getAction() & MotionEvent.ACTION_UP) != 0 && (motionEvent.getActionMasked() & MotionEvent.ACTION_UP) != 0)
            {
                view.getParent().requestDisallowInterceptTouchEvent(false);
            }
            return false;
        }
    });

答案 4 :(得分:0)

试过了吗? :

<EditText
            android:id="@+id/edt_note"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:maxLines="3"
            >

答案 5 :(得分:0)

应用此代码段...您将获得正确的要求。有一点需要注意,你必须给出ScrollView的硬编码高度...我在这里给出了70dip。

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="70dip" >

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:singleLine="false" />
</ScrollView>

答案 6 :(得分:0)

如果textview行超过两行,scrollview将滚动

if (mTvContacts.getLayout().getLineCount() >= 2) {
    if (scrollContactsHeight == 0)
        scrollContactsHeight = scrollContacts.getHeight();

        scrollContacts.getLayoutParams().height = scrollContactsHeight * 2;
} else {
    if (scrollContactsHeight == 0)
        scrollContacts.getLayoutParams().height = scrollContacts.getHeight();
    else
        scrollContacts.getLayoutParams().height = scrollContacts.getHeight() / 2;
}


 <ScrollView  
        android:id="@+id/scrollContacts"
        android:layout_width="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_height="wrap_content"    
        android:layout_marginLeft="5dp"           
        android:layout_marginRight="5dp"        
        android:layout_toRightOf="@+id/tvTo"  
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/imgAddContacts">

        <TextView
            android:id="@+id/tvContacts"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"    
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="wfd"
            android:textColor="#000" />

</ScrollView>