Custom radiobutton, single choice button

Source: Internet
Author: User

Recently I am working on the payment project, but I have seen that the payment option of Meituan is better, so I want to follow it, but it is not that easy to do, therefore, you have rewritten radiogroup and radiobutton to achieve comparison of results.

Meituan: My results:

Although there is still a gap, you can customize the layout to display it. Even if the layout is complicated, you only need to modify some code.

The code below is as follows:

1. This is the custom radiobutton class:

Package COM. JJ. radio; import android. content. context; import android. content. res. typedarray; import android. graphics. drawable. drawable; import android. util. attributeset; import android. view. layoutinflater; import android. widget. checkable; import android. widget. imageview; import android. widget. radiobutton; import android. widget. relativelayout; import android. widget. textview; public class payradiopurified exten DS relativelayout implements checkable {private imageview paylogo; private textview paytitle; private textview paydesc; private radiobutton paychecked; private Boolean mchecked; // whether the private Boolean mbroadcasting; private int ID is selected; private oncheckedchangelistener moncheckedchangewidgetlistener; Public payradiopurified (context, attributeset attrs) {super (context, attrs); layoutinflater Inflater = (Layoutinflater) context. getsystemservice (context. layout_inflater_service); Inflater. inflate (R. layout. payment_list_item, this); paylogo = (imageview) findviewbyid (R. id. pay_icon); paytitle = (textview) findviewbyid (R. id. pay_name); paydesc = (textview) findviewbyid (R. id. pay_desc); paychecked = (radiobutton) findviewbyid (R. id. pay_check); typedarray array = context. obtainstyledattributes (attrs, R. styleabl E. payridiobutton); drawable d = array. getdrawable (R. styleable. payridiobutton_radio); If (D! = NULL) {paychecked. setbuttondrawable (d);} String title = array. getstring (R. styleable. payridiobutton_title); If (title! = NULL) {settexttitle (title);} string STR = array. getstring (R. styleable. payridiobutton_desc); If (STR! = NULL) {settextdesc (STR);} drawable logo = array. getdrawable (R. styleable. payridiobutton_logo); If (LOGO! = NULL) {setdrawablelogo (logo);} Boolean checked = array. getboolean (R. styleable. payridiobutton_checked, false); paychecked. setchecked (checked); array. recycle (); setclickable (true); Id = GETID () ;}@ overridepublic Boolean ischecked () {// todo auto-generated method stubreturn mchecked ;} @ overridepublic void setchecked (Boolean checked) {// todo auto-generated method stubif (mchecked! = Checked) {mchecked = checked; paychecked. refreshdrawablestate (); // avoid infinite recursions if setchecked () is called from a listener if (mbroadcasting) {return;} mbroadcasting = true; If (moncheckedchangewidgetlistener! = NULL) {moncheckedchangewidgetlistener. oncheckedchanged (this, mchecked);} mbroadcasting = false ;}@ overridepublic void toggle () {// todo auto-generated method stubif (! Ischecked () {setchecked (! Mchecked) ;}@ overridepublic Boolean initialize mclick () {// todo auto-generated method stub/** XXX: These are tiny, need some surrounding 'expanded touch region ', * which will need to be implemented in button if we only override * into mclick () * // * when clicked, toggle the State */toggle (); return Super. required mclick ();}/*** register a callback to be invoked when the checked state of this button * Ch Anges. this callback is used for internal purpose only. ** @ Param listener the callback to call on checked state change * @ hide */void setoncheckedchangewidgetlistener (oncheckedchangelistener listener) {moncheckedchangewidgetlistener = listener ;} /*** interface definition for a callback to be invoked when the checked state * of a compound button changed. */public static interface oncheckedch Angelistener {/*** called when the checked state of a compound button has changed. ** @ Param buttonview the compound button view whose state has changed. * @ Param ischecked the new checked state of buttonview. */void oncheckedchanged (payradiopurified buttonview, Boolean ischecked);} public void settextdesc (string s) {If (s! = NULL) {paydesc. settext (s) ;}} public void settexttitle (string s) {If (s! = NULL) {paytitle. settext (s) ;}} Public String gettexttitle () {string S = paytitle. gettext (). tostring (); Return S = NULL? "": S;} public void setdrawablelogo (drawable d) {If (D! = NULL) {paylogo. setimagedrawable (d) ;}} public void setchangeimg (INT checkedid) {system. out. println (">" + checkedid); system. out. println (">" + id); If (checkedid = ID) {paychecked. setchecked (true);} else {paychecked. setchecked (false );}}}

Key points:

(1) layout the parent with the click event setclickable (true );

(2) Use radiobutton to listen for changes in some clicks, that is, paychecked.

(3) Toggle () Method Processing

(4) required mclick () event, must be added!

(5) When changing the background of the radiobutton button, I used GETID () to handle it. It is not good here, but I have limited technology and have not found a better solution,

(6) defects: Poor reusability! For demo only.

2. There are few changes to the radiogroup code. You can modify the code by yourself. The change is the code on the associated payradiopurified,

If you do not understand, you can download the source code.

3. Activity Code:

PayRadioGroup group = (PayRadioGroup) findViewById(R.id.genderGroup);group.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(PayRadioGroup group, int checkedId) {// TODO Auto-generated method stubint radioButtonId = group.getCheckedRadioButtonId();//PayRadioButton rb = (PayRadioButton)MainActivity.this.findViewById(radioButtonId);//Toast.makeText(MainActivity.this, rb.getText(), Toast.LENGTH_SHORT).show();PayRadioPurified rl = (PayRadioPurified)MainActivity.this.findViewById(radioButtonId);for (int i = 0; i < group.getChildCount(); i++) {((PayRadioPurified)group.getChildAt(i)).setChangeImg(checkedId);}Toast.makeText(MainActivity.this, rl.getTextTitle(), Toast.LENGTH_SHORT).show();}});

Note: During the demo implementation, the most basic radiobutton was modified for research. Therefore, the source code contains three reference classes, all of which are test code for self-implemented radiobutton.

Hope to help you:

Portal: Project source code


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.