【问题标题】:Getting text from editText after pressing enter按下回车后从editText获取文本
【发布时间】:2014-05-11 20:39:32
【问题描述】:

我正在尝试使用 android 创建一个应用程序。我使用 EditText 并希望在用户按下其键盘上的“enter”键后从中获取文本。这是 editText 的 XML 代码:

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/nameTxt"
    android:ems="8"
    android:layout_below="@+id/textView"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_marginTop="45dp"
    android:layout_toRightOf="@+id/textView2" />
<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/listView"
    android:layout_below="@+id/textView2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="36dp"
    android:layout_above="@+id/btnClr" />

在我的 .Java 类中,我有以下相关行:

ArrayList<String> listItems=new ArrayList<String>();
ArrayAdapter<String> adapter;

还有那个功能:

public void openAddFriend(View view) {
    String txt;
    final EditText editName;
    editName = (EditText)findViewById(R.id.nameTxt);
    ListView lst;

    lst = (ListView)findViewById(R.id.listView);


    txt = editName.getText().toString();

    listItems.add(txt);
    adapter=new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1,listItems);

    lst.setAdapter(adapter);

}

我的目标是从 EditText 中获取字符串并将其插入到 listView 中。我试过使用 OnKeyListener 但它没有用。有人可以给我一个解决方案的方向吗?提前谢谢你

【问题讨论】:

    标签: java android android-edittext


    【解决方案1】:

    use this

    将 actionid 与适当的 IME_ACTION 匹配(这取决于您在编辑文本上设置的 ime 选项。see)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-13
      • 2014-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-04
      • 2021-12-10
      相关资源
      最近更新 更多