是否可以在gridview中添加单选按钮?或者我可以使复选框像gridview中的单选按钮一样工作吗?
答案 0 :(得分:1)
这是一个例子。它真的可以只使用一个单选按钮。但这是一个应该有所帮助的例子。当然我没有给你一切,但这应该表明你有可能
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
import android.widget.TableLayout;
import android.widget.TableRow;
public class ToggleButton extends TableLayout implements OnClickListener {
private static final String TAG = "ToggleButton";
private RadioButton activeRadioButton;
/**
* @param context
*/
public ToggleButton(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
/**
* @param context
* @param attrs
*/
public ToggleButton(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
public void onClick(View v) {
final RadioButton rb = (RadioButton) v;
///////// do whatever /////////////
}
/* (non-Javadoc)
* @see android.widget.TableLayout#addView(android.view.View, int, android.view.ViewGroup.LayoutParams)
*/
@Override
public void addView(View child, int index,
android.view.ViewGroup.LayoutParams params) {
super.addView(child, index, params);
setChildrenOnClickListener((TableRow)child);
}
/* (non-Javadoc)
* @see android.widget.TableLayout#addView(android.view.View, android.view.ViewGroup.LayoutParams)
*/
@Override
public void addView(View child, android.view.ViewGroup.LayoutParams params) {
super.addView(child, params);
setChildrenOnClickListener((TableRow)child);
}
private void setChildrenOnClickListener(TableRow tr) {
final int c = tr.getChildCount();
for (int i=0; i < c; i++) {
final View v = tr.getChildAt(i);
if ( v instanceof RadioButton ) {
v.setOnClickListener(this);
}
}
}
public int getCheckedRadioButtonId() {
if ( activeRadioButton != null ) {
return activeRadioButton.getId();
}
return -1;
}
}
并创建一个这样的布局(当然你需要清理它,但你有了想法)
<?xml version="1.0" encoding="utf-8"?>
<com.example.android.view.ToggleButton
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="@+id/radGroup1">
<TableRow>
<RadioButton android:id="@+id/rad1" android:text="Button1"
android:layout_width="105px" android:layout_height="wrap_content"
android:textSize="13px" />
<RadioButton android:id="@+id/rad2" android:text="Button2"
android:layout_width="105px" android:textSize="13px"
android:layout_height="wrap_content" />
<RadioButton android:id="@+id/rad3" android:text="Button3"
android:layout_width="105px" android:textSize="13px"
android:layout_height="wrap_content" />
</TableRow>
<TableRow>
<RadioButton android:id="@+id/rad1" android:text="Button1"
android:layout_width="105px" android:layout_height="wrap_content"
android:textSize="13px" />
<RadioButton android:id="@+id/rad2" android:text="Button2"
android:layout_width="105px" android:textSize="13px"
android:layout_height="wrap_content" />
<RadioButton android:id="@+id/rad3" android:text="Button3"
android:layout_width="105px" android:textSize="13px"
android:layout_height="wrap_content" />
</TableRow>
<TableRow>
<RadioButton android:id="@+id/rad1" android:text="Button1"
android:layout_width="105px" android:layout_height="wrap_content"
android:textSize="13px" />
<RadioButton android:id="@+id/rad2" android:text="Button2"
android:layout_width="105px" android:textSize="13px"
android:layout_height="wrap_content" />
<RadioButton android:id="@+id/rad3" android:text="Button3"
android:layout_width="105px" android:textSize="13px"
android:layout_height="wrap_content" />
</TableRow>
</com.example.android.view.ToggleButton>
答案 1 :(得分:0)
我使用了复选框使其正常工作。这里checkedVideo是一个布尔数组,它保存了复选框的值,该复选框被检查。
selectCB.setId(position);
selectCB.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
CheckBox cb = (CheckBox) v;
int id = cb.getId();
Log.i(LOG_TAG, "Clicked ? "+id);
if(checkedVideo[id])
{
checkedVideo[id] = false;
cb.setChecked(false);
}
else
{
for(int index = 0; index < count; index++)
{
checkedVideo[index] = false;
}
checkedVideo[id] = true;
cb.setChecked(true);
notifyDataSetChanged();
}
}
});
Log.i(LOG_TAG, "Checked ? "+checkedVideo[position]);
selectCB.setChecked(checkedVideo[position]);
答案 2 :(得分:0)
你可以制作这样的自定义视图......
public class RadioGridView extends LinearLayout {
private int mClickedPosition = -1;
private Context mContext;
private int[] mItems;
private ImageView[] iv_items;
public RadioGridView(Context context, int[] items) {
super(context);
this.mContext = context;
this.mItems = items;
init();
}
private void init() {
if (mItems != null) {
iv_items = new ImageView[mItems.length];
GridView gv = new GridView(mContext);
addView(gv);
gv.setBackgroundColor(0xffffffff);
gv.setColumnWidth(50);
gv.setNumColumns(GridView.AUTO_FIT);
gv.setVerticalSpacing(10);
gv.setHorizontalSpacing(10);
gv.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
gv.setGravity(Gravity.CENTER);
final RadioGridViewAdapter adapter = new RadioGridViewAdapter();
gv.setAdapter(adapter);
gv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (mClickedPosition >= 0) {
iv_items[mClickedPosition].setBackgroundColor(Color.WHITE);
}
iv_items[position].setBackgroundColor(Color.RED);
mClickedPosition = position;
if (mOnGridItemClickListener != null) {
mOnGridItemClickListener.onItemClick(position);
}
adapter.notifyDataSetChanged();
}
});
}
}
public int getSelectedPosition() {
return mClickedPosition;
}
/*********************************************************
* Listener Interface
********************************************************/
public interface OnGridItemClickListener {
public void onItemClick(int position);
}
public OnGridItemClickListener mOnGridItemClickListener = null;
public void setOnGridItemClickListener(OnGridItemClickListener l) {
mOnGridItemClickListener = l;
}
/*********************************************************
* RadioGidViewAdapter
*********************************************************/
class RadioGridViewAdapter extends BaseAdapter {
@Override
public int getCount() {
return mItems.length;
}
@Override
public Object getItem(int position) {
return mItems[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = new ImageView(mContext);
}
iv_items[position] = (ImageView) convertView;
iv_items[position].setImageResource(mItems[position]);
return convertView;
}
}
}
然后,在这样的活动中使用它。
public class RadioGridActivity extends Activity {
private int[] mItems = {
R.drawable.gv_img_01,
R.drawable.gv_img_02,
R.drawable.gv_img_03,
R.drawable.gv_img_04
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final RadioGridView rgv = new RadioGridView(this, mItems);
setContentView(rgv);
rgv.setOnGridItemClickListener(new OnGridItemClickListener() {
@Override
public void onItemClick(int position) {
Toast.makeText(RadioGridActivity.this, "pos=" + position, 0).show();
}
});
}
}
答案 3 :(得分:0)
我编辑我的代码。 RadioGridView应该像这样改变......
因为Gridview中的第一项未在上面的代码中刷新。
背景mItemsBg应该是第一次设置......嗯...
public class RadioGridView extends LinearLayout {
private int mClickedPosition = -1;
private Context mContext;
private int[] mItems;
private int[] mItemsBg;
private ImageView[] iv_items;
RadioGridViewAdapter adapter;
public RadioGridView(Context context, int[] items) {
super(context);
this.mContext = context;
this.mItems = items;
init();
}
private void init() {
if (mItems != null) {
iv_items = new ImageView[mItems.length];
mItemsBg = new int[mItems.length];
for (int i = 0; i < mItems.length; i++) {
mItemsBg[i] = Color.WHITE;
}
GridView gv = new GridView(mContext);
addView(gv);
gv.setBackgroundColor(0xffffffff);
gv.setColumnWidth(50);
gv.setNumColumns(GridView.AUTO_FIT);
gv.setVerticalSpacing(10);
gv.setHorizontalSpacing(10);
gv.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
gv.setGravity(Gravity.CENTER);
adapter = new RadioGridViewAdapter();
gv.setAdapter(adapter);
gv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (mClickedPosition >= 0) {
mItemsBg[mClickedPosition] = Color.WHITE;
}
mItemsBg[position] = Color.RED;
mClickedPosition = position;
adapter.notifyDataSetChanged();
if (mOnGridItemClickListener != null) {
mOnGridItemClickListener.onItemClick(position);
}
}
});
}
}
/*********************************************************
* Listener Interface
********************************************************/
public interface OnGridItemClickListener {
public void onItemClick(int position);
}
public OnGridItemClickListener mOnGridItemClickListener = null;
public void setOnGridItemClickListener(OnGridItemClickListener l) {
mOnGridItemClickListener = l;
}
/*********************************************************
* RadioGidViewAdapter
*********************************************************/
class RadioGridViewAdapter extends BaseAdapter {
@Override
public int getCount() {
return mItems.length;
}
@Override
public Object getItem(int position) {
return mItems[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = new ImageView(mContext);
}
iv_items[position] = (ImageView) convertView;
iv_items[position].setImageResource(mItems[position]);
iv_items[position].setBackgroundColor(mItemsBg[position]);
return convertView;
}
}
}