歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android 自定義RadioButton(單選按鈕)圖標隨便定

Android 自定義RadioButton(單選按鈕)圖標隨便定

日期:2017/3/1 10:11:46   编辑:Linux編程
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. private int index = 0;
  12. private int id = 0;// 判斷是否選中
  13. private RadioButton tempRadioButton;// 模版用於保存上次點擊的對象
  14. private int state[] = { R.drawable.radio_unchecked,
  15. R.drawable.radio_checked };
  16. /***
  17. * 改變圖片
  18. */
  19. public void ChageImage() {
  20. index++;
  21. id = index % 2;// 獲取圖片id
  22. imageView.setImageResource(state[id]);
  23. }
  24. /***
  25. * 設置文本
  26. *
  27. * @param text
  28. */
  29. public void setText(String text) {
  30. textView.setText(text);
  31. }
  32. public String getText() {
  33. return id == 0 ? "" : textView.getText().toString();
  34. }
  35. public RadioButton(Context context) {
  36. this(context, null);
  37. }
  38. public RadioButton(Context context, AttributeSet attrs) {
  39. super(context, attrs);
  40. this.context = context;
  41. LayoutInflater.from(context).inflate(R.layout.item, this, true);
  42. imageView = (ImageView) findViewById(R.id.iv_item);
  43. textView = (TextView) findViewById(R.id.tv_item);
  44. }
  45. }
上面的實現的很容易,所以不過多解釋.

下面是調用代碼:

  1. public class MainActivity extends Activity {
  2. ListView listView;
  3. @Override
  4. public void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.main);
  7. listView = (ListView) findViewById(R.id.lv_main);
  8. listView.setAdapter(new MyAdapter(this));
  9. }
  10. /***
  11. * @author jia
  12. */
  13. RadioButton temp;
  14. class MyAdapter extends BaseAdapter {
  15. private Context context;
  16. private LayoutInflater inflater;
  17. public MyAdapter(Context context) {
  18. super();
  19. this.context = context;
  20. inflater = LayoutInflater.from(context);
  21. }
  22. @Override
  23. public int getCount() {
  24. return 10;
  25. }
  26. @Override
  27. public Object getItem(int position) {
  28. return null;
  29. }
  30. @Override
  31. public long getItemId(int position) {
  32. return 0;
  33. }
  34. @Override
  35. public View getView(int position, View convertView, ViewGroup parent) {
  36. final RadioButton radioButton;
  37. if (convertView == null) {
  38. radioButton = new RadioButton(context);
  39. } else {
  40. radioButton = (RadioButton) convertView;
  41. }
  42. radioButton.setText(position + "");
  43. radioButton.setOnClickListener(new OnClickListener() {
  44. @Override
  45. public void onClick(View v) {
  46. // 模版不為空,則chage.
  47. if (temp != null) {
  48. temp.ChageImage();
  49. }
  50. temp = radioButton;
  51. radioButton.ChageImage();
  52. Toast.makeText(context, radioButton.getText(), 1000).show();
  53. }
  54. });
  55. return radioButton;
  56. }
  57. }
  58. }
我來說明一下:我們首先創建一個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. }
Copyright © Linux教程網 All Rights Reserved