Permitir varias líneas en la vista EditText en Android?


¿Cómo permitir varias líneas en la vista EditText de Android?

Author: Beryllium, 2010-11-20

12 answers

De forma predeterminada, todos los widgets EditText en Android tienen varias líneas.

Aquí hay un código de ejemplo:

<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 -->
/>
 959
Author: Shardul,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-07-23 05:03:37

Usted puede encontrar que es mejor utilizar:

<EditText 
...
android:inputType="textMultiLine"
/>

Esto se debe a que android:singleLine está obsoleto.

 193
Author: Knossos,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-03-25 09:56:28

Esto funciona para mí, en realidad estos 2 atributos son importantes: inputType y líneas. Además, es posible que necesite una barra de desplazamiento, el siguiente código muestra cómo hacer una:

 <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" />
 36
Author: acoustic,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-06-19 11:50:04

Esta es la forma en que utilicé y también funciona bien. Hope, esto ayudaría a alguien.

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

Salud! ...Gracias.

 11
Author: Nandagopal T,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-03-18 18:53:43

Prueba esto, añade estas líneas a tu vista editar texto, yo añadiré la mía. asegúrate de entenderlo

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

Y en su clase java haga clic en listner a este texto de edición de la siguiente manera, agregaré los nombres míos y chane de acuerdo con los suyos.

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

        });

Esto funciona bien para mí

 9
Author: Chathura Jayanath,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-05-24 14:14:40

EditText tiene la propiedad singleLine. Puede establecer en el XML o llamando a setSingleLine (false); http://developer.android.com/reference/android/widget/TextView.html#setSingleLine%28%29

 8
Author: milan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-11-20 16:10:53

Todos estos son agradables pero no funcionarán en caso de que tenga su edittext dentro de la vista de desplazamiento del nivel superior :) Quizás el ejemplo más común es la vista "Configuración" que tiene tantos elementos que van más allá del área visible. En este caso, los pone a todos en la vista de desplazamiento para hacer que los ajustes se puedan desplazar. En caso de que necesite texto de edición desplazable multilínea en su configuración, su desplazamiento no funcionará.

 5
Author: Cynichniy Bandera,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-02-05 15:46:11

Para deshabilitar el número de líneas que se asignaron previamente en el tema, use el atributo xml: android:lines="@null"

 3
Author: Oleg Bozhko,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-01-24 10:54:39
 <EditText
           android:id="@id/editText" //id of editText
           android:gravity="start"   // Where to start Typing
           android:inputType="textMultiLine" // multiline
           android:imeOptions="actionDone" // Keyboard done button
           android:minLines="5" // Min Line of editText
           android:hint="@string/Enter Data" // Hint in editText
           android:layout_width="match_parent" //width editText
           android:layout_height="wrap_content" //height editText
           /> 
 3
Author: Yogesh Sarvaiya,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-01-19 06:35:08

Aprendí esto de http://www.pcsalt.com/android/edittext-with-single-line-line-wrapping-and-done-action-in-android/, aunque no me gusta el sitio web a mí mismo. Si desea multilínea PERO desea conservar el botón enter como un botón post, establezca el "desplazamiento horizontal" de listview en false.

android:scrollHorizontally="false"

Si no funciona en xml, hacerlo programáticamente funciona extrañamente.

listView.setHorizontallyScrolling(false);

 1
Author: Fire3galaxy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-12-20 06:32:52

Otra cosa interesante es usar android:minHeight junto con android:layout_height="wrap_content". De esta manera, puede establecer el tamaño de la edición cuando está vacía, y cuando el usuario ingresa cosas, se estira de acuerdo con la cantidad de entradas del usuario, incluidas varias líneas.

 0
Author: instanceof,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-07-11 08:37:31
<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" />

Y en la Actividad para eliminar "\n " es el usuario pulse nextline

String editStr= edittxtAddress.getText().toString().trim();

 MyString= editStr.replaceAll("\n"," ");
 0
Author: Kapil Parmar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-12-16 08:14:24