Android User-Defined RadioButton (single-choice button) icon can be customized.

Source: Internet
Author: User

RadioButton is very common in APP development. I don't need to say that everyone knows this. although the Android system provides us with RadioButton, it has a "different" effect for our applications, because android is too rigid and too stone-heavy. you can customize your own icons. next I will introduce you to the implementation method: Method: Use the ImageView and TextView combined control code: /***** control combination ** @ author zhangjia **/public class RadioButton extends LinearLayout {private Context context; private ImageView imageView; private TextView textView; private int index = 0; private int id = 0; // determines whether priva is selected Te RadioButton tempRadioButton; // The template is used to save the private int state [] = {R. drawable. radio_unchecked, R. drawable. radio_checked};/***** change image */public void ChageImage () {index ++; id = index % 2; // obtain image id imageView. setImageResource (state [id]);}/***** set the text ** @ param text */public void setText (String text) {textView. setText (text);} public String getText () {return id = 0? "": TextView. getText (). toString ();} public RadioButton (Context context) {this (context, null);} public RadioButton (Context context, AttributeSet attrs) {super (context, attrs); this. context = context; LayoutInflater. from (context ). inflate (R. layout. item, this, true); imageView = (ImageView) findViewById (R. id. iv_item); textView = (TextView) findViewById (R. id. TV _item) ;}} the above implementation is very easy, so there are more explanations. the following is the call code.: Public class MainActivity extends Activity {ListView listView; @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); listView = (ListView) findViewById (R. id. lv_main); listView. setAdapter (new MyAdapter (this);}/***** @ author jia */RadioButton temp; class MyAdapter extends BaseAdapter {private Context context Context; private Layout Inflater inflater; public MyAdapter (Context context) {super (); this. context = context; inflater = LayoutInflater. from (context) ;}@ Override public int getCount () {return 10 ;}@ Override public Object getItem (int position) {return null ;} @ Override public long getItemId (int position) {return 0 ;}@ Override public View getView (int position, View convertView, ViewGroup parent) {final RadioButton RadioButton; if (convertView = null) {radioButton = new RadioButton (context);} else {radioButton = (RadioButton) convertView;} radioButton. setText (position + ""); radioButton. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {// The template is not empty, then chage. if (temp! = Null) {temp. chageImage ();} temp = radioButton; radioButton. chageImage (); Toast. makeText (context, radioButton. getText (), 1000 ). show () ;}}); return radioButton ;}}let me explain: First, we create a temp template to remember the RadioButton object you clicked. when you click, first check whether temp is null. If not empty, run temp. chageImage (); this method cancels the selection. if it is not null, The RadioButton is first executed to cancel the selection of the button. after executing the RadioButton ChageImage method you clicked, remember to pay the current RadioButton to temp. effect:
The effect is achieved, but there is a small problem, because currently only 10 pieces of data cannot see the effect. If you change to 20, you will find a strange problem. Figure ": 15th pieces of data will be automatically checked, found again, and finally found, because of the listview problem. See final RadioButton radioButton; if (convertView = null) {radioButton = new RadioButton (context);} else {radioButton = (RadioButton) convertView;} You may find out, in order to improve efficiency, listview convertView is reused. so this bug will occur, and the solution is very simple. We just need to replace the above Code with final RadioButton radioButton; radioButton = new RadioButton (context); although this is a little less efficient, but sometimes we need to break it down, as long as it can achieve the effect, and occasionally put down the performance of the water is also OK, not to mention this situation can not have so many columns. project implementation style:
It looks like it's okay. Here I will upload the Code. If there are deficiencies, I can adjust it myself. I just provide some ideas. source code download [color = ize: 18px] [color = ize: 18px] additional extensions: [color = ize: 18px]/********************************** **************************************** /[color = ize: 18px] LayoutInflater. from (context ). inflate (R. layout. item, this); [color = ize: 18px] View = LayoutInflater. from (context ). inflate (R. layout. item, null); [color = ize: 18px] the above two methods must be familiar to everyone. LayoutInflater is indispensable for custom views. So what is the difference? I have never understood it before, including reading this article and reading it, and I am better than Hulu. public View inflate (int Resourece, ViewGroup root) function: Fill in a new View hierarchy from the specified XML reSource file reSource: View layout IDroot: the Root View of the hierarchy that is filled with the return value of the Root View of the hierarchy generated. If the root parameter is provided, root is the root view; otherwise, the root of the filled XML file is the root view. In my simple explanation, when the root is null, we only convert an xml file instance into a View object, and the reverse is the View corresponding to xml. when root is not null, parent exists. then we will instantiate this xml View object and add this View to its parent. so here we use LayoutInflater. from (context ). inflate (R. layout. item, this); this is actually to instantiate XML as a part of our own, so when we call this control, we will display the view we want (XML view ). everyone understands it here. in this way, you will not be confused when using it in the future. for details, refer to this article: One of the source code analyses of the View framework: How android creates a view. the explanation is quite thorough. After understanding it, we will be able to develop it in the future. (* ^__ ^ *). /*************************************** * ***********************************/second methods: modify the style specified by RadioButton to see the configuration file seletor. xml and style. xml <selector xmlns: android = "http://schemas.android.com/apk/res/android"> <item android: drawable = "@ drawable/radio_checked" android: state_checked = "true" android: state_enabled = "true"> </item> <item android: drawable = "@ drawable/radio_unchecked" android: state_checked = "false" android: state_enabled = "true"> </item> </selector> <resources xmlns: android = "http://schemas.android.com/apk/res/android"> <style name = "RadioButtonStyles"> <item name = "android: button "> @ drawable/selector </item> </style> </resources>. <RadioGroup android: id = "@ + id/rg_main" android: layout_width = "match_parent" android: layout_height = "wrap_content"> <RadioButton android: id = "@ + id/button1" style = "@ style/RadioButtonStyles" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: text = "5 K"/> <RadioButton android: id = "@ + id/button2" style = "@ style/RadioButtonStyles" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: text = "10 K"/> </RadioGroup> This method is simple, but I don't think the scope of the application is wide as defined above, for example, in my project above, there are two text items in RadioButton, so we can't implement it with RadioButton. It is better to select Custom in the case of complicated RadioButton. this is so much for RadioButton, and I hope it will help you.

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.