i have read several posts has same questions do:
- when add onclicklistener view (in case edittext) inside fragment class, click event never fired.
- if move code away fragment class , move activity instead, click event fired correctly.
the other questions ask same question, solutions have (if any) not apply situation can tell. therefore, asking question "again" =)
first, fragment_main.xml:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:orientation="vertical" android:paddingbottom="@dimen/activity_vertical_margin" tools:context="se.snapcode.lanstrafiken.mainactivityfragment"> <linearlayout android:focusable="true" android:focusableintouchmode="true" android:layout_width="0px" android:layout_height="0px"/> <edittext android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputtype="textpostaladdress" android:ems="15" android:id="@+id/edittextfrom" android:hint="from" android:layout_alignparentend="true" android:focusableintouchmode="false" android:clickable="true" android:layout_marginbottom="20sp" /> </linearlayout> and mainactivityfragment.java:
public class mainactivityfragment extends fragment { public mainactivityfragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view myview = inflater.inflate(r.layout.fragment_main, container, false); edittext ed1 = (edittext) myview.findviewbyid(r.id.edittextfrom); ed1.settext("this test"); ed1.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { log.d("snapcode", "test 1 2 3"); toast.maketext(v.getcontext(), "test", toast.length_long).show(); } }); edittext ed2 = (edittext) myview.findviewbyid(r.id.edittextto); ed1.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { } }); return myview; } } when click edittext, log.d(...) not show in logcat @ all, , toast isnt displayed.
now, if remove has edittext fragment, , place in activity instead, works:
public class mainactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar1); setsupportactionbar(toolbar); edittext ed1 = (edittext) findviewbyid(r.id.edittextfrom); ed1.settext("test again"); ed1.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { log.d("snapcode", "test activity"); toast.maketext(mainactivity.this, "test", toast.length_long).show(); } }); } // ... } any ideas?
sidenote: think bad design android code isnt "encapsulated". can see above, can access controls inside fragment outside fragment, if controls in same scope. r.id.edittextfrom available everywhere, find disturbing. should available inside fragment, unless explicitly make "public". compare usercontrols in .net - there usercontrols standalone. havent tried, happens if 2 separate fragments have same id control? smells how web works (and webcomponents trying solve).
i found problem. easy one, fault:

so, yeah... bad, nothing see here, move right along =)
Comments
Post a Comment