2012-07-12 101 views
1

是否可以在gridview中添加單選按鈕?或者,我可以使複選框像GridView中的單選按鈕工作?gridview中的單選按鈕

+2

不知您懷疑到底是什麼? – 2012-07-12 13:10:48

+0

Actully我想在gridview中添加單選按鈕,但它似乎是somthing不可能,所以我怎麼可以使用複選框在網格視圖中像單選按鈕一樣工作? – 2012-07-12 13:14:00

+0

在我的應用程序中,用戶可以從gridview中顯示的多個視頻中檢查一個視頻,我想添加單選按鈕/複選框以供選擇 – 2012-07-12 13:14:58

回答

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> 
+0

謝謝獵鷹,我用複選框使它工作。雖然,我還沒有使用你的代碼,但我接受你的答案,因爲它使用單選按鈕,它是完美的:) – 2012-07-12 14:09:22

+0

<33謝謝。我之前使用Check Boxes作爲單選按鈕(當我第一次在Android中開始編程時遇到'問題'),並且工作正常。它需要處理器稍微思考一些,但它確實有效,並且不會耗盡電池電量。希望你不會遇到更多的問題!祝你好運! – Falcon165o 2012-07-12 14:10:31

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]); 
0

可以使這樣的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(); 
     } 
    }); 



    } 

} 
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; 
    } 

    } 
}