EditText focus dont work(android) -
sorry english. have 3 edittext , and in scrollview. when click on 1 of edittext, scrollview descends down , focuses on edittext chose. if click edittext of 3 scroll down , focus edittext city
no matter chose edittext. not know why, ask help
this xml have 3 edittext city
, adress
, zip
<linearlayout android:layout_weight="0.1" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginleft="5dp" android:layout_marginright="5dp"> <linearlayout android:layout_width="match_parent" android:layout_height="50dp" android:background="#26ffffff" android:layout_marginbottom="5dp" > <edittext android:id="@+id/adress" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@null" android:layout_gravity="center" android:ems="15" android:gravity="center" android:hint="adress" android:singleline="true" android:textcolor="#c9c9c9" android:textcolorhint="#c9c9c9" android:layout_centervertical="true" android:layout_centerhorizontal="true" /> </linearlayout> <linearlayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="50dp"> <linearlayout android:layout_width="match_parent" android:layout_height="50dp" android:layout_weight="0.2" android:layout_marginright="2.5dp" android:background="#26ffffff" android:layout_marginbottom="10dp" > <edittext android:id="@+id/zip" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@null" android:layout_gravity="center" android:ems="15" android:gravity="center" android:hint="zip" android:singleline="true" android:textcolor="#c9c9c9" android:textcolorhint="#c9c9c9" android:layout_centervertical="true" android:layout_centerhorizontal="true" /> </linearlayout> <linearlayout android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginleft="2.5dp" android:layout_weight="0.2" android:background="#26ffffff" android:layout_marginbottom="10dp" > <edittext android:id="@+id/city" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@null" android:layout_gravity="center" android:ems="15" android:gravity="center" android:hint="city" android:singleline="true" android:textcolor="#c9c9c9" android:textcolorhint="#c9c9c9" android:layout_centervertical="true" android:layout_centerhorizontal="true" /> </linearlayout> </linearlayout>
my code
//adress = (edittext) findviewbyid(r.id.adress); // zip = (edittext) findviewbyid(r.id.zip); // city = (edittext) findviewbyid(r.id.city); adress.getviewtreeobserver().addongloballayoutlistener(new viewtreeobserver.ongloballayoutlistener() { @override public void ongloballayout() { iskeyboardshown(adress.getrootview(), adress); zip.clearfocus(); city.clearfocus(); }}); zip.getviewtreeobserver().addongloballayoutlistener(new viewtreeobserver.ongloballayoutlistener() { @override public void ongloballayout() { iskeyboardshown(zip.getrootview(), zip); adress.clearfocus(); city.clearfocus(); }}); city.getviewtreeobserver().addongloballayoutlistener(new viewtreeobserver.ongloballayoutlistener() { @override public void ongloballayout() { iskeyboardshown(city.getrootview(), city); zip.clearfocus(); adress.clearfocus(); }}); //method, if keyboard show -> scroll down private boolean iskeyboardshown(view rootview, textview text) { /* 128dp = 32dp * 4, minimum button height 32dp , generic 4 rows soft keyboard */ final int soft_keyboard_height_dp_threshold = 128; rect r = new rect(); rootview.getwindowvisibledisplayframe(r); displaymetrics dm = rootview.getresources().getdisplaymetrics(); /* heightdiff = rootview height - status bar height (r.top) - visible frame height (r.bottom - r.top) */ int heightdiff = rootview.getbottom() - r.bottom; /* threshold size: dp pixels, multiply display density */ boolean iskeyboardshown = heightdiff > soft_keyboard_height_dp_threshold * dm.density; log.e( "iskeyboardshown ? " + iskeyboardshown + ", heightdiff:" + heightdiff + ", density:" + dm.density + "root view height:" + rootview.getheight() + ", rect:" + r, "sd"); //check keyboard show or not if(iskeyboardshown) { scroll.fullscroll(view.focus_down); text.requestfocus(); } else { } return iskeyboardshown; }
udp
now code is
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); adress = (edittext) findviewbyid(r.id.adress); zip = (edittext) findviewbyid(r.id.zip); city = (edittext) findviewbyid(r.id.city); scroll = (scrollview) findviewbyid(r.id.scrollview3); //down scroll view downscroll(); //focuses edit text focusedittext(); } //its method create public void focusedittext() { adress.setontouchlistener(new view.ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { log.i("click", "onmtouch"); adress.setfocusable(true); adress.requestfocus(); zip.clearfocus(); city.clearfocus(); return false; } }); zip.setontouchlistener(new view.ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { log.i("click", "onmtouch"); zip.setfocusable(true); zip.requestfocus(); adress.clearfocus(); city.clearfocus(); return false; } }); city.setontouchlistener(new view.ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { log.i("click", "onmtouch"); city.setfocusable(true); city.requestfocus(); zip.clearfocus(); adress.clearfocus(); return false; } }); } //scroll down public void downscroll() { adress.getviewtreeobserver().addongloballayoutlistener(new viewtreeobserver.ongloballayoutlistener() { @override public void ongloballayout() { iskeyboardshown(adress.getrootview(), adress); }}); zip.getviewtreeobserver().addongloballayoutlistener(new viewtreeobserver.ongloballayoutlistener() { @override public void ongloballayout() { iskeyboardshown(zip.getrootview(), zip); }}); city.getviewtreeobserver().addongloballayoutlistener(new viewtreeobserver.ongloballayoutlistener() { @override public void ongloballayout() { iskeyboardshown(city.getrootview(), city); }}); } //method scroll down private boolean iskeyboardshown(view rootview, edittext text) { /* 128dp = 32dp * 4, minimum button height 32dp , generic 4 rows soft keyboard */ final int soft_keyboard_height_dp_threshold = 128; rect r = new rect(); rootview.getwindowvisibledisplayframe(r); displaymetrics dm = rootview.getresources().getdisplaymetrics(); /* heightdiff = rootview height - status bar height (r.top) - visible frame height (r.bottom - r.top) */ int heightdiff = rootview.getbottom() - r.bottom; /* threshold size: dp pixels, multiply display density */ boolean iskeyboardshown = heightdiff > soft_keyboard_height_dp_threshold * dm.density; log.e( "iskeyboardshown ? " + iskeyboardshown + ", heightdiff:" + heightdiff + ", density:" + dm.density + "root view height:" + rootview.getheight() + ", rect:" + r, "sd"); if(iskeyboardshown) { scroll.fullscroll(view.focus_down); } else { if (!adresstext.equals("") && !citytext.equals("") && isonline()) { new geocodertask().execute(new string[]{citytext + "," + adresstext}); } } return iskeyboardshown; }
Comments
Post a Comment