Android 自定义RadioButton(单选按钮)图标随便定


RadioButton在我们开发APP应用中是很常见的.这点我不用说大家也心知肚明. 

虽说Android 系统给我们提供了RadioButton但是为了我们的应用有种"与众不同"的效果,因为android的太死板太斯通见惯了.往往都会定制自己的图标.下面我给大家介绍一下我实现的方法:

方法:运用组合控件(ImageView and TextView

组合控件代码: 

  1. /*** 
  2.  * 组合控件 
  3.  *  
  4.  * @author zhangjia 
  5.  *  
  6.  */  
  7. public class RadioButton extends LinearLayout {  
  8.     private Context context;  
  9.     private ImageView imageView;  
  10.     private TextView textView;  
  11.   
  12.     private int index = 0;  
  13.     private int id = 0;// 判断是否选中   
  14.   
  15.     private RadioButton tempRadioButton;// 模版用于保存上次点击的对象   
  16.   
  17.     private int state[] = { R.drawable.radio_unchecked,  
  18.             R.drawable.radio_checked };  
  19.   
  20.   
  21.     /*** 
  22.      * 改变图片 
  23.      */  
  24.     public void ChageImage() {  
  25.   
  26.         index++;  
  27.         id = index % 2;// 获取图片id   
  28.         imageView.setImageResource(state[id]);  
  29.     }  
  30.   
  31.     /*** 
  32.      * 设置文本 
  33.      *  
  34.      * @param text 
  35.      */  
  36.     public void setText(String text) {  
  37.         textView.setText(text);  
  38.     }  
  39.   
  40.     public String getText() {  
  41.         return id == 0 ? "" : textView.getText().toString();  
  42.   
  43.     }  
  44.   
  45.     public RadioButton(Context context) {  
  46.         this(context, null);  
  47.   
  48.     }  
  49.   
  50.     public RadioButton(Context context, AttributeSet attrs) {  
  51.         super(context, attrs);  
  52.         this.context = context;  
  53.         LayoutInflater.from(context).inflate(R.layout.item, thistrue);  
  54.         imageView = (ImageView) findViewById(R.id.iv_item);  
  55.         textView = (TextView) findViewById(R.id.tv_item);  
  56.   
  57.     }  
  58.   
  59. }  
上面的实现的很容易,所以不过多解释.

下面是调用代码:

  1. public class MainActivity extends Activity {  
  2.     ListView listView;  
  3.   
  4.     @Override  
  5.     public void onCreate(Bundle savedInstanceState) {  
  6.         super.onCreate(savedInstanceState);  
  7.         setContentView(R.layout.main);  
  8.         listView = (ListView) findViewById(R.id.lv_main);  
  9.         listView.setAdapter(new MyAdapter(this));  
  10.     }  
  11.   
  12.     /*** 
  13.      * @author jia 
  14.      */  
  15.     RadioButton temp;  
  16.   
  17.     class MyAdapter extends BaseAdapter {  
  18.         private Context context;  
  19.         private LayoutInflater inflater;  
  20.   
  21.         public MyAdapter(Context context) {  
  22.             super();  
  23.             this.context = context;  
  24.             inflater = LayoutInflater.from(context);  
  25.         }  
  26.   
  27.         @Override  
  28.         public int getCount() {  
  29.             return 10;  
  30.         }  
  31.   
  32.         @Override  
  33.         public Object getItem(int position) {  
  34.             return null;  
  35.         }  
  36.   
  37.         @Override  
  38.         public long getItemId(int position) {  
  39.             return 0;  
  40.         }  
  41.   
  42.         @Override  
  43.         public View getView(int position, View convertView, ViewGroup parent) {  
  44.             final RadioButton radioButton;  
  45.             if (convertView == null) {  
  46.                 radioButton = new RadioButton(context);  
  47.             } else {  
  48.                 radioButton = (RadioButton) convertView;  
  49.             }  
  50.   
  51.             radioButton.setText(position + "");  
  52.   
  53.             radioButton.setOnClickListener(new OnClickListener() {  
  54.                 @Override  
  55.                 public void onClick(View v) {  
  56.                     // 模版不为空,则chage.   
  57.                     if (temp != null) {  
  58.                         temp.ChageImage();  
  59.                     }  
  60.                     temp = radioButton;  
  61.                     radioButton.ChageImage();  
  62.   
  63.                     Toast.makeText(context, radioButton.getText(), 1000).show();  
  64.   
  65.                 }  
  66.             });  
  67.   
  68.             return radioButton;  
  69.         }  
  70.     }  
  71. }  
我来说明一下:我们首先创建一个temp模版,用于记忆你点击的那个RadioButton对象.  在你点击时候,首先查看temp是否为null,如果不为空则执行 temp.ChageImage(); 这个方法是取消选中效果.如果不为null,则首先对该RadioButton执行,取消该按钮选中状态.在执行你点击的那个RadioButton的ChageImage方法,最后记得要把当前的RadioButton付给temp.

 效果:

    

效果是实现了,不过有个小问题,因为目前只有10条数据是看不出效果的.换成20条你就会发现很诡异的问题。

图“:


第15条数据会自动勾选上,找了又找,最后终于发现了,是因为listview 的问题。看下面:

  1. final RadioButton radioButton;  
  2.             if (convertView == null) {  
  3.                 radioButton = new RadioButton(context);  
  4.             } else {  
  5.                 radioButton = (RadioButton) convertView;  
  6.             }  
  • 1
  • 2
  • 下一页

相关内容