输入键在EditText中开始一个新行而不是提交文本(Android)

Tom*_*ila 8 android android-edittext

我有一个简单的todolist应用程序我正在创建,通过EditText视图添加新的待办事项.因此,当我按下回车键时,我正试图将新项目提交到列表中.出于某种原因,它只是输入了一个新的行.我也注意到当我通过AVD通过eclipse 使用回车键时它工作正常,但问题只发生在我通过我的Nexus 4运行时.

这是我的代码:

@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;
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

和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"  
/>
Run Code Online (Sandbox Code Playgroud)

请注意我在堆栈溢出时发现了类似的问题,但似乎没有解决我的问题.我试过了!

Mob*_*Mon 14

将EditText小部件上的singleLine属性设置为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"
/>
Run Code Online (Sandbox Code Playgroud)


Pri*_*ary 5

android:inputType="textMultiLine"
Run Code Online (Sandbox Code Playgroud)

在edittext中添加此属性将解决此问题