Introduzca la clave que comienza una nueva línea en EditText en lugar de enviar el texto (Android)

Tengo una aplicación todolist simple que estoy creando por lo que nuevos elementos de todo se agregan a través de una vista EditText. Así que estoy tratando de tener que enviar el nuevo elemento a la lista cuando presiono la tecla Intro . Por alguna razón, aunque acaba de introducir una nueva línea en su lugar . También he notado que funciona bien cuando uso la tecla de entrada a través de un AVD a través de eclipse, pero el problema sólo se produce cuando estoy corriendo a través de mi Nexus 4.

Aquí está mi código:

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.todo_main); //Create reference to the ListView in the main layout ListView myListView = (ListView)findViewById(R.id.myListView); //Create a reference to the EditText view in the main layout for adding new items final EditText editText = (EditText)findViewById(R.id.myEditText); //Create a an arraylist to hold all the todo items final ArrayList<String> todoList = new ArrayList<String>(); //Create an ArrayAdaptor object to be able to bind ArrayLists to ListViews final ArrayAdapter<String> arrayAdaptor = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, todoList); myListView.setAdapter(arrayAdaptor); //Listens for certain keys to be pushed, particular the dpad center key or enter key editText.setOnKeyListener(new View.OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if(event.getAction() == KeyEvent.ACTION_DOWN) { if((keyCode == KeyEvent.KEYCODE_ENTER || keyCode == KeyEvent.KEYCODE_DPAD_CENTER)) { //add item in the EditText to the todo list todoList.add(0, editText.getText().toString()); //Update the view by notifying the ArrayAdapter of the data changes arrayAdaptor.notifyDataSetChanged(); editText.setText(""); return true; } return false; } return false; } }); } 

Y XML:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".ToDoListActivity"> <EditText android:id="@+id/myEditText" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/addItemHint" android:imeOptions="actionDone" /> <ListView android:id="@+id/myListView" android:layout_width="match_parent" android:layout_height="wrap_content" /> 

Tenga en cuenta que he encontrado preguntas similares sobre el desbordamiento de pila, pero ninguno parecen resolver mi problema. ¡He intentado!

Establezca la propiedad singleLine en el widget EditText como true:

 <EditText android:singleLine="true" android:id="@+id/myEditText" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/addItemHint" android:imeOptions="actionDone" /> 
 android:inputType="textMultiLine" 

La adición de esta propiedad en edittext resolverá el problema

 android:inputType="textImeMultiLine" 

En mi caso esto ha resuelto el problema!

Android: singleLine = "true" es depricated, use

Android: maxLines = "1"

Android: inputType = "texto"

Android: imeOptions = "actionNext"

  • EditText Settext no funciona con Fragment
  • ScrollView salta a EditText
  • Mostrar el último carácter de una contraseña en un EditText
  • El ajuste de EditText imeOptions a actionNext no tiene efecto cuando se usan dígitos
  • Configuración de EditText con "url" inputType
  • Volver evento clave en EditText
  • Android: Establecer Editar texto o ver la vista de texto Programáticamente
  • Android EditText - evento de mecanografía terminado
  • Cómo crear una contraseña entera EditText mediante programación
  • Android: ¿Cómo puedo recuperar Edittext.getText () en AlertDialog personalizado?
  • Establecer estilo de texto de sugerencia Editarxts como cursiva
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.