是否可以在gridview中添加單選按鈕?或者,我可以使複選框像GridView中的單選按鈕工作?gridview中的單選按鈕
回答
繼承人一個例子。它真的可以使用單選按鈕。但這是一個應該幫助的例子。當然,我沒有給你的一切,但這個應該告訴你有可能
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>
謝謝獵鷹,我用複選框使它工作。雖然,我還沒有使用你的代碼,但我接受你的答案,因爲它使用單選按鈕,它是完美的:) – 2012-07-12 14:09:22
<33謝謝。我之前使用Check Boxes作爲單選按鈕(當我第一次在Android中開始編程時遇到'問題'),並且工作正常。它需要處理器稍微思考一些,但它確實有效,並且不會耗盡電池電量。希望你不會遇到更多的問題!祝你好運! – Falcon165o 2012-07-12 14:10:31
我用複選框使它工作。這裏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]);
可以使這樣的customview ...
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();
}
});
}
}
我編輯我的代碼。 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;
}
}
}
- 1. gridview中的單選按鈕
- 2. asp.net gridview單選按鈕
- 3. Yii gridview中的單選按鈕
- 4. 單選按鈕中的單選按鈕
- 5. 在ASP.Net中選擇一個單選按鈕的gridview行
- 6. gridview和單選按鈕列表瀏覽器後退按鈕
- 7. 選中的單選按鈕
- 8. gridview中的按鈕
- 9. 隱藏GridView中的選擇按鈕
- 10. 更改GridView中的選擇按鈕
- 11. 在javascript中的gridview行中查找單選按鈕列表
- 12. 如何在gridview中使用按鈕單擊中的單選按鈕獲取所選行的值
- 13. 存儲GridView控件的多重選擇的單選按鈕的?
- 14. 單單選按鈕選擇在GridView控件
- 15. 單選按鈕像單選按鈕
- 16. Gridview中的html單選按鈕。啓用/ disble的列值
- 17. 在渲染時更改gridview中單選按鈕的名稱
- 18. 確認GridView事件中的單選按鈕不會觸發
- 19. 如何查找Gridview中的單選按鈕控件
- 20. 從gridview中的單選按鈕觸發異步回調
- 21. 如何選擇一個單選按鈕後選擇一個gridview
- 22. Javascript單選按鈕 - 試圖只選中選中的按鈕
- 23. GridView中的單擊按鈕禁用其他按鈕
- 24. 檢查並更改單選按鈕組中的單選按鈕
- 25. 單選按鈕中的單選按鈕行格柵視圖
- 26. 沒有正確獲取單選按鈕中的單選按鈕
- 27. WatiN:如何檢查單選按鈕組中的單選按鈕
- 28. 在gridview中的單選按鈕上單擊時顯示一個複選框
- 29. Gridvew中的單選按鈕
- 30. SharedPreferences中的單選按鈕?
不知您懷疑到底是什麼? – 2012-07-12 13:10:48
Actully我想在gridview中添加單選按鈕,但它似乎是somthing不可能,所以我怎麼可以使用複選框在網格視圖中像單選按鈕一樣工作? – 2012-07-12 13:14:00
在我的應用程序中,用戶可以從gridview中顯示的多個視頻中檢查一個視頻,我想添加單選按鈕/複選框以供選擇 – 2012-07-12 13:14:58