android - setOnKeyListener not responding -
i new android , working through list example book. have 1 activity displaying edittext , listview beneath it. there onkey event should add text edittext listview , clear edittext. however, when hit enter on keyboard happens new line added edittext , nothing added listview.
the oncreate code is:
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_to_do_list); // ui references listview mylistview = (listview)findviewbyid(r.id.mylistview); final edittext myedittext = (edittext)findviewbyid(r.id.myedittext); myedittext.settext("test"); // create arraylist store items final arraylist<string> todoitems = new arraylist<string>(); // create arrayadapter bind items list view final arrayadapter<string> aa; aa = new arrayadapter<string>(this, android.r.layout.simple_list_item_1, todoitems); // bind adapter 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; } }); }
i've added myedittext.settext("test");
ensure reference myedittext working, is. i've tried removing if statements onkey event , doesn't appear registering key events @ all. can advise i'm doing wrong here?
you try using oneditoractionlistener instead:
you set on edittext in xml:
android:imeoptions="actiondone"
and in code:
myedittext.setoneditoractionlistener(new edittext.oneditoractionlistener() { @override public boolean oneditoraction(textview v, int actionid, keyevent event) { if (actionid == editorinfo.ime_action_done) { // action here return true; } return false; } )};
Comments
Post a Comment