2016-07-07 47 views
-3

嗨我正在做一个学校项目,我正在做一个性别分类。如果我选择男性,男性图标应加载到我的android应用程序中的imageView和Vice Versa。如何选择单选按钮将图像加载到imageView

public static TextView lable5,lable7; 
public static SeekBar SB1,SB2; 
public static RadioButton RB1,RB2; 
public static ImageView IV1; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_layout); 

    lable5 = (TextView) findViewById(R.id.textView5); //this is for seekbar1 
    lable7 = (TextView) findViewById(R.id.textView7); //This is for seekbar2 
    RB1 = (RadioButton) findViewById(R.id.radioButton1); // This is for male option 
    RB2 = (RadioButton) findViewById(R.id.radioButton2); // This is for Female Option 
    IV1 = (ImageView) findViewById(R.id.imageView); // This is the only imageView available 


    SB1=(SeekBar) findViewById(R.id.seekBar1);// This is for seekBar1 
    SB2=(SeekBar) findViewById(R.id.seekBar2);//This is for seekBar2 
    lable5.setText(String.valueOf(SB1.getProgress())); 
} 
+2

你到目前为止做了什么?告诉我们你的代码。 – SripadRaj

+0

欢迎来到StackOverflow。请阅读[如何提出一个好问题。](http://stackoverflow.com/help/how-to-ask)并注意:Java和JavaScript是独立的语言。 – nnnnnn

+0

以上显示 –

回答

0

假设你添加的单选按钮在RadioGroup你可以做到以下几点:

group = (RadioGroup) findViewById(R.id.radioGroup); 
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
    @Override 
    public void onCheckedChanged(RadioGroup group, int checkedId) { 
    switch(checkedId) { 
     case R.id.radioButton1: 
     IV1.setImageResource(R.drawable.male_icon); 
     break; 
     case R.id.radioButton2: 
     IV1.setImageResource(R.drawable.female_icon); 
     break; 
     default: 
     break; 
    } 
    } 
}); 

如果你的单选按钮在RadioGroup你可以用这样的代码添加他们:

<RadioGroup 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/radioGroup" > 
    <RadioButton ... /> 
    <RadioButton ... /> 
</RadioGroup> 

如果您在使用我的代码时遇到问题,请发表评论,我会帮您。

+0

这是在广播组,我得到一个错误,说int不能转换为Drawable –

+0

我的坏。对于那个很抱歉!我更新了答案,它现在应该可以工作。问题是我使用了'setImageDrawable',它有一个'Drawable'参数,我发送了一个'int'。使用'setImageResource'你可以通过它的id来设置图像。 –

+0

谢谢这是工作的晶圆厂:D –