android - How Can I put a error msg if there is a blank field on Java? -
i trying make unit converter , seems working need give them error if don't put number on first field , other error if put number on second field (so cant convert backwards)
public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); final edittext editcentimeters = (edittext) findviewbyid(r.id.editcentimeters); final edittext editinches = (edittext) findviewbyid(r.id.editinches); button buttonconvert = (button)findviewbyid(r.id.buttonconvert); buttonconvert.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { double centimeters = double.valueof( editcentimeters.gettext().tostring()); double inches = centimeters * 0.393700787 ; editinches.settext(string.valueof(inches)); } }); } }
use seterror() method this. somthing this
buttonconvert.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { if(editcentimeters.gettext().tostring().equals("")){ editcentimeters.seterror("*field required"); }else{ double centimeters = double.valueof( editcentimeters.gettext().tostring()); double inches = centimeters * 0.393700787 ; editinches.settext(string.valueof(inches)); } } });
Comments
Post a Comment