Allow multi-line in EditText view in Android?

前端 未结 15 1582
既然无缘
既然无缘 2020-11-28 17:40

How to allow multi-line in Android\'s EditText view?

相关标签:
15条回答
  • 2020-11-28 17:53
    <EditText
                    android:hint="Address"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:inputType="textMultiLine|textNoSuggestions"
                    android:id="@+id/edittxtAddress"
                    android:layout_marginLeft="@dimen/textsize2"
                    android:textColor="@android:color/white"
                    android:scrollbars="vertical"
                    android:minLines="2"
                android:textStyle="normal" />
    

    and in Activity to remove "\n" is user press nextline

    String editStr= edittxtAddress.getText().toString().trim();
    
     MyString= editStr.replaceAll("\n"," ");
    
    0 讨论(0)
  • 2020-11-28 17:56

    This is how I applied the code snippet below and it's working fine. Hope, this would help somebody.

    <EditText 
        android:id="@+id/EditText02"
        android:gravity="top|left" 
        android:inputType="textMultiLine"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:lines="5" 
        android:scrollHorizontally="false" 
    />
    

    Cheers! ...Thanks.

    0 讨论(0)
  • 2020-11-28 17:57

    By default all the EditText widgets in Android are multi-lined.

    Here is some sample code:

    <EditText
        android:inputType="textMultiLine" <!-- Multiline input -->
        android:lines="8" <!-- Total Lines prior display -->
        android:minLines="6" <!-- Minimum lines -->
        android:gravity="top|left" <!-- Cursor Position -->
        android:maxLines="10" <!-- Maximum Lines -->
        android:layout_height="wrap_content" <!-- Height determined by content -->
        android:layout_width="match_parent" <!-- Fill entire width -->
        android:scrollbars="vertical" <!-- Vertical Scroll Bar -->
    />
    
    0 讨论(0)
  • This works for me, actually these 2 attributes are important: inputType and lines. Besides, you may need a scrollbar, the code below shows how to make one:

     <EditText
            android:id="@+id/addr_edittext"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="top|left"
            android:inputType="textEmailAddress|textMultiLine"
            android:lines="20"
            android:minLines="5"
            android:scrollHorizontally="false"
            android:scrollbars="vertical" />
    
    0 讨论(0)
  • 2020-11-28 18:02

    Try this, add these lines to your edit text view, i'll add mine. make sure you understand it

    android:overScrollMode="always"
    android:scrollbarStyle="insideInset"
    android:scrollbars="vertical"
    
    <EditText
        android:inputType="textMultiLine"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editText_newprom_description"
        android:padding="10dp"
        android:lines="5"
        android:overScrollMode="always"
        android:scrollbarStyle="insideInset"
        android:minLines="5"
        android:gravity="top|left"
        android:scrollbars="vertical"
        android:layout_marginBottom="20dp"/>
    

    and on your java class make on click listner to this edit text as follows, i'll add mine, chane names according to yours.

    EditText description;
    description = (EditText)findViewById(R.id.editText_newprom_description);
    
    description.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_UP:
                            view.getParent().requestDisallowInterceptTouchEvent(false);
                            break;
                    }
    
                    return false;
                }
    
            });
    

    this works fine for me

    0 讨论(0)
  • 2020-11-28 18:02

    EditText has singleLine property. You can set in the XML or by calling setSingleLine(false); http://developer.android.com/reference/android/widget/TextView.html#setSingleLine%28%29

    0 讨论(0)
提交回复
热议问题