android - Combined Control :ImageButton and TextView,i want the ImageButton can response click event,not the TextView,however,i can not do it -
package com.example.test20140308; import android.content.context; public class mainbutton extends linearlayout { private drawable drawable; private drawable drawablebg; public imagebutton mimage; textview mtext; private int size; string text = ""; public mainbutton(context context) { super(context); } public mainbutton(context context, attributeset attrs) { super(context, attrs); typedarray localtypedarray = context.obtainstyledattributes(attrs, r.styleable.mybutton); if (localtypedarray.hasvalue(r.styleable.mybutton_mybuttonicon)) { this.drawable = localtypedarray .getdrawable(r.styleable.mybutton_mybuttonicon); } if (localtypedarray.hasvalue(r.styleable.mybutton_mybackground)) { this.drawablebg = localtypedarray .getdrawable(r.styleable.mybutton_mybackground); } if (localtypedarray.hasvalue(r.styleable.mybutton_mybuttonsize)) { this.size = ((int) localtypedarray.getdimension( r.styleable.mybutton_mybuttonsize, 0.0f)); } this.mimage = new imagebutton(context, attrs); this.mimage.setimagedrawable(this.drawable); this.mimage.setadjustviewbounds(true); this.mimage.setbackgrounddrawable(this.drawablebg); this.mimage.setlayoutparams(new linearlayout.layoutparams( getresources().getinteger(r.integer.imagebutton_w), getresources().getinteger(r.integer.imagebutton_h))); this.mimage.setclickable(true); this.mimage.setfocusable(true); this.mtext = new textview(context, attrs); this.mtext.setpadding(0, getresources().getinteger(r.integer.padding_top), 0, 0); this.mtext.settextsize(this.size); this.mtext.settextcolor(color.rgb(81, 81, 81)); this.mtext.setgravity(1); this.mtext.setclickable(false); this.mimage.setfocusable(false); setorientation(1); addview(this.mimage); addview(this.mtext); localtypedarray.recycle(); } }
i try creat combined control app using imagebutton , textview,and want imagebutton can response click event , textview not.howerer,when try use in app,i found result opposite,only when touch textview combined control can response.below activity code:
package com.example.test20140308; import android.app.activity; import android.os.bundle; import android.view.menu; import android.view.view; import android.view.view.onclicklistener; import android.widget.toast; public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mainbutton buutonbutton = (mainbutton) findviewbyid(r.id.btnsavedetail1); buutonbutton.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { // todo auto-generated method stub toast.maketext(mainactivity.this, "cc", toast.length_long) .show(); } }); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } }
my english not enough,maybe confused,please point out,thank you!
Comments
Post a Comment