setOnKeyListener not responding

后端 未结 6 1028
执念已碎
执念已碎 2021-01-05 17:43

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 ev

6条回答
  •  再見小時候
    2021-01-05 18:21

    I was able to get that same example to work by adding the following attribute to the EditText element android:inputType="text". This changed the software keyboard that came up for the user and included a Send button.

    In the setOnKeyListener code, I changed it to the following:

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

    Don't use KEYCODE_DPAD_CENTER as that's a hardware button for devices that had up, down, left, right arrows.

提交回复
热议问题