一.效果图:
二.布局
sms_model.xml
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:id="@+id/sms_model_list"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1.45"
/>
android:id="@+id/confirm_btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/confirm" />
sms_model_item.xml
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
android:id="@+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:focusable="false"/>
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
三.Activity代码
package com.example.listviewandradiobutton;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
public class MainActivity extends Activity {
ListView listView;
Button confirmBtn;
ListAdapter listAdapter;
List list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sms_model);
Book[] books = new Book[] { new Book("java", "you", 2011),
new Book("c++", "him", 2012), new Book("android", "she", 2013) };
list = new ArrayList();
for(Book book:books){
list.add(book);
}
listView = (ListView) findViewById(R.id.sms_model_list);
confirmBtn = (Button) findViewById(R.id.confirm_btn);
listAdapter = new ListAdapter(MainActivity.this);
listAdapter.setList(list);
listView.setAdapter(listAdapter);
final Intent intent = new Intent(MainActivity.this,NextActivity.class);
intent.putExtra("selectedBook", "你还什么都没有选择!");
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view,
int position, long id) {
listAdapter.select(position);
Book book = (Book)listAdapter.getItem(position);
String value = book.getBookName() + " " + book.getAuthor()
+ " " + book.getPublishTime();
intent.putExtra("selectedBook", value);
}
});
confirmBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(intent);
}
});
}
}
package com.example.listviewandradiobutton;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class NextActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String value = getIntent().getExtras().getString("selectedBook");
TextView textView = new TextView(this);
textView.setText(value);
setContentView(textView);
}
}
四.实体类代码
package com.example.listviewandradiobutton;
public class Book{
private String bookName;
private String author;
private int publishTime;
private boolean isSelected = false;
public Book(){};
public Book(String bookName,String author,int publishTime){
this.bookName = bookName;
this.author = author;
this.publishTime = publishTime;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getPublishTime() {
return publishTime;
}
public void setPublishTime(int publishTime) {
this.publishTime = publishTime;
}
public boolean isSelected() {
return isSelected;
}
public void setSelected(boolean isSelected) {
this.isSelected = isSelected;
}
}
五.ListAdapter代码
package com.example.listviewandradiobutton;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RadioButton;
import android.widget.TextView;
public class ListAdapter extends BaseAdapter {
Context context;
LayoutInflater mInflater;
ViewHolder holder;
List Book;
Book book;
public ListAdapter(Context context) {
this.context = context;
this.mInflater = LayoutInflater.from(context);
}
public void setList(List Book) {
this.Book = Book;
}
// 选中当前选项时,让其他选项不被选中
public void select(int position) {
if (!Book.get(position).isSelected()) {
Book.get(position).setSelected(true);
for (int i = 0; i < Book.size(); i++) {
if (i != position) {
Book.get(i).setSelected(false);
}
}
}
notifyDataSetChanged();
}
@Override
public int getCount() {
return Book.size();
}
@Override
public Object getItem(int position) {
return Book.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.sms_model_item, null);
holder.radioBtn = (RadioButton) convertView
.findViewById(R.id.radioButton);
holder.radioBtn.setClickable(false);
holder.textView = (TextView) convertView
.findViewById(R.id.textView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
book = (Book) getItem(position);
holder.radioBtn.setChecked(book.isSelected());
holder.textView.setText(book.getBookName() + " " + book.getAuthor()
+ " " + book.getPublishTime());
return convertView;
}
class ViewHolder {
RadioButton radioBtn;
TextView textView;
}
}
六.AndroidManifest.xml中添加NextActivity
package="com.example.listviewandradiobutton"
android:versionCode="1"
android:versionName="1.0" >
android:minSdkVersion="8"
android:targetSdkVersion="17" />
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:name="com.example.listviewandradiobutton.MainActivity"
android:label="@string/app_name" >
android:name="com.example.listviewandradiobutton.NextActivity"
android:label="@string/app_name" >
七.也算上吧,values包中的strings.xml
ListViewAndRadioButton
Settings
Hello world!
确定
八.代码下载地址