gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:52:31 26 4
gpt4 key购买 nike

我正在创建一个简单的待办事项应用程序,通过 EditText View 添加新的待办事项。所以我试图让它提交新项目到列表当我按下回车键。但出于某种原因,它只是换行。我还注意到,当我通过 Eclipse 通过 AVD 使用回车键时,它工作正常,但问题仅在我通过 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;
}
});
}

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

请注意,我在堆栈溢出上发现了类似的问题,但似乎没有一个能解决我的问题。我试过了!

最佳答案

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

关于android - 在 EditText 中输入开始新行的键而不是提交文本 (Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20474321/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com