許久沒(méi)有寫(xiě)過(guò)博客了,近來(lái)在做Android開(kāi)發(fā),突然想到這里,以后可以在這里貼些代碼,做些記錄,與大家分享交流。Android開(kāi)發(fā)中,常常會(huì)用到RadioButton與ListView的混合使用,用戶點(diǎn)擊一條Item,然后記錄下選中的狀態(tài),其中最重要的是記錄好用戶點(diǎn)擊選中Item的位置。
目前創(chuàng)新互聯(lián)已為千余家的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務(wù)器托管、綿陽(yáng)服務(wù)器托管、企業(yè)網(wǎng)站設(shè)計(jì)、衛(wèi)濱網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
布局文件很簡(jiǎn)單:
ItemList也不復(fù)雜:
RadioButtonList 類(lèi),這里我寫(xiě)了一些自己喜歡的作家(當(dāng)然我也很喜歡和大家交流一些文學(xué)作品)作為模擬數(shù)據(jù)。
package com.example.radiobuttonlisttest; import android.app.Activity; import android.os.Bundle; import android.widget.ListView; public class RadioButtonList extends Activity { private ListView radioButtonList; private RadioAdapter adapter; // 模擬幾個(gè)數(shù)據(jù),作為L(zhǎng)ist的條目 private String[] authors = { "芥川龍之介", "三島由紀(jì)夫", "川端康成", "村上春樹(shù)", "東野圭吾", "張愛(ài)玲", "金庸", "錢(qián)鐘書(shū)", "老舍", "梁實(shí)秋", "亨利米勒", "海明威", "菲茲杰拉德", "凱魯亞克", "杰克倫敦", "小仲馬", "杜拉斯", "福樓拜", "雨果", "巴爾扎克", "莎士比亞", "勞倫斯", "毛姆", "柯南道爾", "笛福" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_radio_button_list); radioButtonList = (ListView) findViewById(R.id.list); adapter = new RadioAdapter(this, authors); radioButtonList.setAdapter(adapter); } }
適配器是最關(guān)鍵的,標(biāo)記好選擇的位置,選中狀態(tài)不會(huì)因?yàn)長(zhǎng)istView的滑動(dòng)而出現(xiàn)混亂:
package com.example.radiobuttonlisttest; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.RadioButton; import android.widget.TextView; import android.widget.Toast; public class RadioAdapter extends BaseAdapter { private LayoutInflater inflater; private String[] authors; private viewHolder holder; // 標(biāo)記用戶當(dāng)前選擇的那一個(gè)作家 private int index = -1; private Context c; public RadioAdapter(Context c, String[] authors) { super(); this.c = c; this.authors = authors; inflater = LayoutInflater.from(c); } @Override public int getCount() { return authors.length; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(final int position, View convertView, ViewGroup parent) { holder = new viewHolder(); if (convertView == null) { convertView = inflater.inflate(R.layout.item_list, null); holder.nameTxt = (TextView) convertView.findViewById(R.id.author); holder.selectBtn = (RadioButton) convertView .findViewById(R.id.radio); convertView.setTag(holder); } else { holder = (viewHolder) convertView.getTag(); } holder.nameTxt.setText(authors[position]); holder.selectBtn .setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { Toast.makeText(c, "您選擇的作家是:" + authors[position], Toast.LENGTH_LONG).show(); index = position; notifyDataSetChanged(); } } }); if (index == position) {// 選中的條目和當(dāng)前的條目是否相等 holder.selectBtn.setChecked(true); } else { holder.selectBtn.setChecked(false); } return convertView; } public class viewHolder { public TextView nameTxt; public RadioButton selectBtn; } }
最后看看效果圖: