RadioButton在我們開發APP應用中是很常見的.這點我不用說大家也心知肚明. 雖說Android 系統給我們提供了RadioButton但是為了我們的應用有種"與眾不同"的效果,因為android的太死板太斯通見慣了.往往都會定製自己的表徵圖.下面我給大家介紹一下我實現的方法: 方法:運用群組控制項(ImageView and TextView) 群組控制項代碼: /**** 群組控制項* * @author zhangjia* */public class RadioButton extends LinearLayout {private Context context;private ImageView imageView;private TextView textView; private int index = 0;private int id = 0;// 判斷是否選中 private RadioButton tempRadioButton;// 模版用於儲存上次點擊的對象 private int state[] = { R.drawable.radio_unchecked, R.drawable.radio_checked }; /*** * 改變圖片 */public void ChageImage() { index++; id = index % 2;// 擷取圖片id imageView.setImageResource(state[id]);} /*** * 設定文本 * * @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); } }上面的實現的很容易,所以不過多解釋. 下面是調用代碼: 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; private LayoutInflater 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) { // 模版不為空白,則chage. if (temp != null) { temp.ChageImage(); } temp = radioButton; radioButton.ChageImage(); Toast.makeText(context, radioButton.getText(), 1000).show(); } }); return radioButton; }}}我來說明一下:我們首先建立一個temp模版,用於記憶你點擊的那個RadioButton對象. 在你點擊時候,首先查看temp是否為null,如果不為空白則執行 temp.ChageImage(); 這個方法是取消選中效果.如果不為null,則首先對該RadioButton執行,取消該按鈕選中狀態.在執行你點擊的那個RadioButton的ChageImage方法,最後記得要把當前的RadioButton付給temp. 效果:
效果是實現了,不過有個小問題,因為目前只有10條資料是看不出效果的.換成20條你就會發現很詭異的問題。 圖“: 第15條資料會自動勾選上,找了又找,最後終於發現了,是因為listview 的問題。看下面: final RadioButton radioButton; if (convertView == null) { radioButton = new RadioButton(context); } else { radioButton = (RadioButton) convertView; } 也許你會發現了,因為我們為了提高效率,重用了listview個convertView.所以會出現這種bug,解決方案也很簡單,只需要我們把上面代碼更換為 final RadioButton radioButton; radioButton = new RadioButton(context); 雖說這樣效率有點低,但是有時候我們需要則斷一下,只要能實現效果,偶爾對效能放下水也是OK的,何況這種情況下不可能有那麼多列. 項目實現樣式:
看起來還湊合吧。 這裡我把代碼上傳一下,不足的地方,自己可以進行調整,我只是提供個思路. 源碼下載 [color=ize:18px] [color=ize:18px]額外拓展: [color=ize:18px]/*****************************************************************************/ [color=ize:18px]LayoutInflater.from(context).inflate(R.layout.item, this); [color=ize:18px]View view=LayoutInflater.from(context).inflate(R.layout.item, null); [color=ize:18px]上面兩個方法想必大家在熟悉不過了,自訂View的時候離不開LayoutInflater這個東東,那麼有什麼區別呢,之前我一直不明白,包括寫這篇文章的時候,也是看了別人這麼搞,自己就比葫蘆畫瓢了. public View inflate(int Resourece,ViewGroup root)作用:填充一個新的視圖階層從指定的XML資源檔中reSource:View的layout的IDroot: 產生的階層的根視圖return 填充的階層的根視圖。如果參數root提供了,那麼root就是根視圖;否則填充的XML檔案的根就是根視圖。 我簡單解釋下:當root為null的時候,我們只是把一個xml檔案執行個體化成View對象,反回的就是xml對應的View.而當root不為null的時候,也就是存在parent.那麼我們將把這個xml執行個體化程View對象後,將這個View視圖add進其parent中.所以在這裡我們用的是LayoutInflater.from(context).inflate(R.layout.item, this);這樣其實就是把XML執行個體化後當作自己的一部分,這樣我們在調用此控制項的時候,顯示的就是我們想要的那個視圖了(XML視圖).說到這裡大家明白了.這樣以後用的時候再也不會糊塗了.如果想詳細瞭解那麼請參考這篇文章:View視圖架構源碼分析之一:android是如何建立一個view.講解的那是的相當的透徹,看懂後對於以後我們開發是百利而無一害啊.(*^__^*) . /*****************************************************************************/ 第二種方法:對RadioButton給定樣式進行改造 看設定檔 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>最後只需要在RadioButton中引用即可. <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="5K" /> <RadioButton android:id="@+id/button2" style="@style/RadioButtonStyles" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="10K" /> </RadioGroup> 這種方式很簡單吧,不過我覺的應用範圍沒有上面自訂來的廣,比如說上面我做的項目中,RadioButton中的text有兩項,這樣我們用RadioButton就無法實現了,遇到比較複雜的RadioButton選擇自訂是比較好的. 針對RadioButton 就說這麼多了,希望對你有協助.