java - unable to match edittext with image name -
learning android making simple guessing game, have set images in arraylist , applied random operation on it, i'm confused how match edit text value image name can set score.
public class mainactivity extends appcompatactivity { private edittext edittext; private button button; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); arraylist<integer> list = new arraylist<integer>(); imageview random_image = (imageview) findviewbyid(r.id.imageview); final int[] images = {r.drawable.kung_fu_panda, r.drawable.mr_nobody, r.drawable.toy_story}; list.add(r.drawable.kung_fu_panda); list.add(r.drawable.mr_nobody); list.add(r.drawable.toy_story); int position = new random().nextint(list.size()); random_image.setimageresource((integer) list.get(position)); list.remove(position); button = (button) findviewbyid(r.id.button); edittext = (edittext) findviewbyid(r.id.edittext); button.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { validateinput(); } }); } private void validateinput() { string input = edittext.gettext().tostring(); }
you can try definition name's array of images, , name random position, , use currentname
save name.
string currentname; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); arraylist<integer> list = new arraylist<integer>(); imageview random_image = (imageview) findviewbyid(r.id.imageview); final string imagenames = {"kung fu panda", "mr nobody", "toy story"}; list.add(r.drawable.kung_fu_panda); list.add(r.drawable.mr_nobody); list.add(r.drawable.toy_story); int position = new random().nextint(list.size()); random_image.setimageresource((integer) list.get(position)); currentname = imagenames[position]; list.remove(position); button = (button) findviewbyid(r.id.button); edittext = (edittext) findviewbyid(r.id.edittext); button.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { validateinput(); } }); }
then, in validateinput()
method, can use currentname
compare, notice case, use string.tolowercase()
format input string.
private boolean validateinput() { string input = edittext.gettext().tostring(); if(!textutils.isempty(input)){ input = input..tolowercase(); if(input.equeals(currentname)){ return true; } } return false; }
Comments
Post a Comment