2013-12-09 4 views
7

Ich habe eine einfache Todolist-App, die ich erstelle, wobei neue Todo-Elemente über eine EditText-Ansicht hinzugefügt werden. Also versuche ich es zu haben den neuen Artikel in die Liste einreichen, wenn ich die Eingabetaste drücke. Aus irgendeinem Grund jedoch gibt es nur eine neue Zeile statt. Ich habe auch bemerkt, dass es feinen funktioniert, wenn ich die Schlüssel über eine AVD durch Eclipse eingeben verwenden, aber das Problem tritt nur auf, wenn ich es durch mein Nexus renne 4.Geben Sie den Schlüssel ein, um eine neue Zeile in EditText zu erstellen, anstatt den Text zu senden (Android)

Hier ist mein Code:

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

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

Bitte beachten Sie, dass ich ähnliche Fragen zum Stack-Überlauf habe, aber keine scheint mein Problem zu lösen. Ich habe es versucht!

Antwort

13

Stellen Sie die single Eigenschaft auf Ihrem EditText Widget auf 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" 
/> 
+0

android: singleLine ist veraltet. – FractalBob

2
android:inputType="textMultiLine" 

diese Eigenschaft in EditText Zugabe wird das Problem

0
android:inputType="textImeMultiLine" 

In meinem Fall das Problem gelöst haben zu lösen!

0

android: single = "true" wird depricated verwenden

android: maxLines = "1"

android: input = "text"

android: imeOptions = "actionNext"

Verwandte Themen