2011-12-05 52 views
0

在我的應用我有查看這樣的形象:如何使它在android中選擇瓶子成爲可能?

enter image description here

現在,我要做出這樣coading的是,一個瓶子應該被選中。 如果我選擇另一瓶,那麼應該選擇該瓶並且應該取消選擇另一瓶。 它應該爲所有的瓶子而發生。

我知道我可以使用RadioGroup和RadioButton for Bottle。

但取而代之的是有沒有其他的方式來使它成爲可能? 如果是,那麼請給我一點技巧。

+0

爲什麼你去當你做複雜的事情這個東西用單選按鈕? – Pratik

+0

我認爲這是可以實現自定義畫廊視圖 – 2011-12-05 13:10:33

+0

你可以嘗試GridView ...推薦是RadioGroup –

回答

2

這裏是一個很好的例子(靈感): http://vikaskanani.wordpress.com/2011/07/20/android-custom-image-gallery-with-checkbox-in-grid-to-select-multiple/

onCreate(){ 
GridView imagegrid = (GridView) findViewById(R.id.phoneimagegrid); 
     imageAdapter = new ImageAdapter(this); 
     imagegrid.setAdapter(imageAdapter); 
     imagegrid.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, View v, 
        int position, long id) { 
       Toast.makeText(AndroidCustomGalleryActivity.this, 
         "" + position, Toast.LENGTH_SHORT).show(); 
      } 
     }); 
} 

public class ImageAdapter extends BaseAdapter { 
... 
public View getView(int position, View convertView, ViewGroup parent) { 
      ImageView imageView; 
      if (convertView == null) { // if it's not recycled, initialize some 
             // attributes 
       imageView = new ImageView(mContext); 
       imageView.setLayoutParams(new GridView.LayoutParams(120, 120)); 
       imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
       imageView.setPadding(30, 8, 30, 8); 
      } else { 
       imageView = (ImageView) convertView; 
      } 

      imageView.setImageResource(mThumbIds[position]); 
      return imageView; 
     } 

     // references to our images 
     private Integer[] mThumbIds = { R.drawable.icon, R.drawable.icon, 
       R.drawable.icon, R.drawable.icon, R.drawable.icon, 
       R.drawable.icon, R.drawable.icon, R.drawable.icon }; 
    } 

佈局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <GridView 
     android:id="@+id/phoneimagegrid" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:columnWidth="120dp" 
     android:gravity="center" 
     android:horizontalSpacing="10dp" 
     android:numColumns="auto_fit" 
     android:stretchMode="columnWidth" 
     android:verticalSpacing="10dp" /> 

</RelativeLayout> 
+0

不錯的一個例子。讓我試試看。 –

+1

這裏很好的例子。但我想看到按鈕/圖像應保持選定,直到我按下另一個按鈕/圖像。 –

+0

@iDroidExplorer你只需要模仿按鈕或按鈕本身的效果? – 2011-12-06 10:44:08

1

您可以手動完成。將所有按鈕放在數組中,並在onclick監聽器中選擇它並取消選擇數組中的所有其他按鈕。

希望這有助於...

相關問題