java - Adding multiple on click listeners for several buttons -
how can make user can have several buttons on click listeners. tried 2 buttons, said had defined on-click listener not make on click listener result. code have far is:
import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.support.v4.app.fragmentactivity; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmenttransaction; import android.view.view; import android.widget.linearlayout; public class mainactivity extends appcompatactivity { private view btnrender; private linearlayout container; private view btnrendered; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); btnrender = (view)findviewbyid(r.id.btn_render); container = (linearlayout)findviewbyid(r.id.fragment_layout); btnrendered = (view) findviewbyid(r.id.btn_rendered); //set event handling button btnrender.setonclicklistener(onclicklistener()); } private view.onclicklistener onclicklistener() { return new view.onclicklistener() { @override public void onclick(view v) { //replace fragment when clicked fragmentmanager fm = getsupportfragmentmanager(); fragmenttransaction ft = fm.begintransaction(); ft.replace(r.id.fragment_layout, new pdfrenderfragment()); ft.commit(); //gone button after btnrender.setvisibility(view.gone); container.setvisibility(view.visible); } }; } }
the btn_rendered
other button want set on click listener for, software not let me this. true need set switch-case method work?
i tried out both codes , none of them seem have doen desired effect. whenever click 1 of buttons, 2nd pdf shows! (what using button for)
you can handle below.
btnrender.setonclicklistener(onclicklistener()); btnrendered.setonclicklistener(onclicklistener()); private view.onclicklistener onclicklistener() { return new view.onclicklistener() { @override public void onclick(view v) { switch(v.getid()){ case r.id.btn_render: //replace fragment when clicked fragmentmanager fm = getsupportfragmentmanager(); fragmenttransaction ft = fm.begintransaction(); ft.replace(r.id.fragment_layout, new pdfrenderfragment()); ft.commit(); //gone button after btnrender.setvisibility(view.gone); container.setvisibility(view.visible); break; case r.id.btn_rendered: // logic here break; } } }; }
Comments
Post a Comment