setOnKeyListener没有响应 [英] setOnKeyListener not responding

查看:161
本文介绍了setOnKeyListener没有响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Andr​​oid,并通过合作,从一本书待办事项例子。我有这显示一个EditText和它下面的一个ListView一个活动。有是应该从EditText上添加文本到ListView和清除的EditText的安其事件。然而,当我按下回车键在键盘上所发生的一切是一个新行被添加到EditText上并没有什么被添加到ListView。

I am new to Android and working through a to do list example from a book. I have one Activity which is displaying an EditText and a ListView beneath it. There is an onKey event which should add the text from the EditText to the ListView and clear the EditText. However, when I hit Enter on the keyboard all that happens is a new line is added to the EditText and nothing is added to the ListView.

本的onCreate code是:

The onCreate code is:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_to_do_list);

    // Get UI references
    ListView myListView = (ListView)findViewById(R.id.myListView);
    final EditText myEditText = (EditText)findViewById(R.id.myEditText);
    myEditText.setText("test");
    // Create ArrayList to store To Do items
    final ArrayList<String> toDoItems = new ArrayList<String>();

    // Create ArrayAdapter to bind items to List View
    final ArrayAdapter<String> aa;

    aa = new ArrayAdapter<String>(this,
                                  android.R.layout.simple_list_item_1,
                                  toDoItems);

    // Bind Adapter to List View
    myListView.setAdapter(aa);

    myEditText.setOnKeyListener(new View.OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN)
                if ((keyCode == KeyEvent.KEYCODE_DPAD_CENTER) ||
                    (keyCode == KeyEvent.KEYCODE_ENTER)) {
                    toDoItems.add(0, myEditText.getText().toString());
                    aa.notifyDataSetChanged();
                    myEditText.setText("");
                    return true;
                }
            return false;
        }
    });
}

我已经添加了 myEditText.setText(测试); 只是为了确保引用myEditText工作,这是。我已经试过所有取出如果从安其事件的陈述和它只是似乎并没有被登记的关键事件。任何人都可以告诉我在做什么错在这里?

I've added the myEditText.setText("test"); just to ensure the reference to myEditText is working, which it is. I've tried removing the if statements from the onKey event and it just doesn't appear to be registering key events at all. Can anyone advise what I'm doing wrong here?

推荐答案

您可以尝试使用OnEditorActionListener代替:

You could try using a OnEditorActionListener instead:

<一个href=\"http://developer.android.com/reference/android/widget/TextView.html#setOnEditorActionListener(android.widget.TextView.OnEditorActionListener)\">http://developer.android.com/reference/android/widget/TextView.html#setOnEditorActionListener(android.widget.TextView.OnEditorActionListener)

您可以在XML中的EditText设置:

You could set on the edittext in XML:

的android:imeOptions =actionDone

,然后在code:

 myEditText.setOnEditorActionListener(
    new EditText.OnEditorActionListener() {
  @Override
  public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
      if (actionId == EditorInfo.IME_ACTION_DONE) {
        // your action here
      }
   }
 }
 )};

这篇关于setOnKeyListener没有响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆