Ho una semplice applicazione per i todol che sto creando in base alla quale nuovi elementi todo vengono aggiunti tramite una vista EditText. Quindi sto cercando di farlo inviare il nuovo articolo alla lista quando premo il tasto di invio. Per qualche motivo, sebbene sia , viene semplicemente inserita una nuova riga invece. Ho anche notato che funziona bene quando uso il tasto Invio tramite un AVD attraverso eclissi, ma il problema si verifica solo quando sono in esecuzione attraverso il mio Nexus 4.Immettere il tasto di avvio di una nuova riga in EditText invece di inviare il testo (Android)
Ecco il mio codice:
@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;
}
});
}
E 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"
/>
Si prega di notare che ho trovato domande simili sullo stack overflow ma nessuna sembra risolvere il mio problema. Ho provato!
android: singleLine è stato dichiarato obsoleto. – FractalBob