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;
-
- @Override
- public 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;
- }