android 重写ListView实现RaidoButton单选

分享到:



1,MainActivity的代码

package com.excetop.listradio;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
private ListView listView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ListView) this.findViewById(R.id.list);
MyAdapter adapter = new MyAdapter();
listView.setAdapter(adapter);
}
private class MyAdapter extends BaseAdapter{
private String[] s = new String[]{"a","b","c","d","e"};
private int temp = -1;

@Override
public int getCount() {
// TODO Auto-generated method stub
return s.length;
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder;
if(convertView == null){
convertView = MainActivity.this.getLayoutInflater().inflate(R.layout.item, null);
holder = new Holder();
convertView.setTag(holder);
}else{
holder = (Holder) convertView.getTag();
}

holder.button = (Button) convertView.findViewById(R.id.button);
holder.button.setText(s[position]);

holder.radioButton = (RadioButton) convertView.findViewById(R.id.radioButton);
holder.radioButton.setId(position);
holder.radioButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

if(isChecked){
if(temp != -1){
RadioButton tempButton = (RadioButton) MainActivity.this.findViewById(temp);
tempButton.setChecked(false);

}

temp = buttonView.getId();
Log.i(TAG,"you are women- - " + isChecked + " " + temp);

}
}
});
return convertView;
}
private class Holder{
private Button button;
private RadioButton radioButton;
}
}
}

2,mian.xml的代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>

3,item.xml的代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试">

</Button>

<RadioButton
android:id="@+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

</LinearLayout>

昵    称:
验证码:

相关文档: