-1

我想用单选按钮显示图像并用广播组显示,因为用户可以通过阅读文本和查看图像来选择选项。我怎样才能使这成为可能?以编程方式合并单选按钮和图像浏览

这是创建单选按钮的代码,也是动态显示图像视图的代码,但是当我单击单选按钮选择它时显示错误,因为图像视图不能转换为单选按钮,所以这些代码会在广播组中创建。在广播组中也使得图像视图成为一个小孩,并且它不工作,但显示完美。

public RadioGroup showNationalCan(RadioGroup rg,Context context,ImageView iv,String voterNA){ 

    //candidate address as punjab kpk etc 

    if(conn==null){ 

    } 
    try{ 
     RadioButton rb; 
     Statement st=conn.createStatement(); 
     ResultSet rs=st.executeQuery("select * from AM_NATIONAL where ca_na=N'"+voterNA+"'"); 
     while (rs.next()) { 
      rb=new RadioButton(context); 
      rb.setTextColor(Color.BLACK); 
      rb.setId(rs.getInt(1)); 
      rb.setText(rs.getString(2)+"\n"+rs.getString(3)+"\n"); 

      iv=new ImageView(context); 
      byte[] photo=rs.getBytes(4); 
      Bitmap bitmap; 
      bitmap=BitmapFactory.decodeByteArray(photo, 0, photo.length); 
      iv.setImageBitmap(bitmap); 
      iv.setEnabled(false); 


      rg.addView(iv); 
      rg.addView(rb); 
     } 
     rs.close(); 
     st.close(); 

    } catch (SQLException e){ 
     e.printStackTrace(); 
    } 
    return rg; 
} 
+0

为什么我的问题是投票?我需要理由。我的问题有什么问题吗?我需要知道。可能这会帮助我。因为这没有任何理由不害怕这样做。 – Awais 2014-09-02 13:47:27

回答

0

这是我自己的答案也就是现在的工作绝对完美。我在这里回答了这个问题,因为其他人可以做到这一点,并能够像SQL一样直接从外部数据库设置图像。

rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(RadioGroup group, int checkedId) { 
       // TODO Auto-generated method stub 
       for(int i=0; i<rg.getChildCount();i++) 
       { 
        RadioButton rbtn=(RadioButton)rg.getChildAt(i+1); 
        if(rbtn.getId()==checkedId) 
        { 
         serial=rbtn.getId(); 
         flag=true; 
         return; 
        } 
        else 
        { 
         rbtn.setChecked(false); 
        } 
       } 

      } 
     }); 

问题是电台组有图像视图在它作为孩子,这不会被单击作为单选按钮。我只是用i + 1增加i,因为它每次都会得到单选按钮。

2

您可以通过drawableRight属性设置图像。你会从思考链接中获得更多细节。

http://developer.android.com/reference/android/widget/TextView.html#attr_android:drawableRight

<RadioButton 
     android:id="@+id/radio0" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:checked="true" 
     android:drawableRight="@drawable/ic_launcher" 
     android:text="A" > 
    </RadioButton> 

您可以使用此动态添加单选按钮。

RadioGroup rgp= (RadioGroup) findViewById(R.id.radiogroup); 
     RadioGroup.LayoutParams rprms; 

     for(int i=0;i<3;i++){ 
       RadioButton radioButton = new RadioButton(this); 
       radioButton.setText("new"+i); 
       radioButton.setId(i); 
       radioButton.setChecked(true); 
       radioButton.setCompoundDrawables(drwable_left, drwable_top, drwable_right,     drwable_bottom); 
       rprms= new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
       rgp.addView(radioButton, rprms); 
     } 

可以在setCompoundDrawables

设置位图
setCompoundDrawables(new BitmapDrawable(your_left_bitmap), new BitmapDrawable(your_top_bitmap), new BitmapDrawable(your_right_bitmap), new BitmapDrawable(your_bottom_bitmap)); 

更新:

Bitmap drawable = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 

       radioButton.setCompoundDrawables(new BitmapDrawable(drawable), new BitmapDrawable(drawable), new BitmapDrawable(drawable), new BitmapDrawable(drawable)); 
+0

,但单选按钮是动态创建的,图像视图也是由sql中的数据库创建的,我通过jdbc连接它们。我想以编程方式设置它们 – Awais 2014-09-02 14:26:34

+0

感谢您的回答亲爱的@DhrubaBose这非常明显,因为没有人回答它。但是当我们在android drawable中有图像时,使用setCompundDrawables方法,而不是像我那样直接从数据库中设置的图像。我的问题是,当我们点击收音机组来获取单选按钮应用程序崩溃时,由于图像查看。 – Awais 2014-09-03 11:44:15

+0

您可以在setCompoundDrawables方法中使用位图。检查我的更新。 – 2014-09-03 12:14:49