把Android里从 RadioButton和CheckButton 中获取的显示在一个TextView里!求解!

2024-11-25 12:37:01
推荐回答(1个)
回答1:

public class RadioGroup extends Activity
{
public TextView mTextView1;
public RadioGroup mRadioGroup1;
public RadioButton mRadio1,mRadio2;
private String result; //用于获取结果

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

/*取得 TextView、RadioGroup、RadioButton对象*/
mTextView1 = (TextView) findViewById(R.id.myTextView);
mRadioGroup1 = (RadioGroup) findViewById(R.id.RadioGroup);
mRadio1 = (RadioButton) findViewById(R.id.man);
mRadio2 = (RadioButton) findViewById(R.id.woman);

/*RadioGroup用OnCheckedChangeListener来运行*/
mRadioGroup1.setOnCheckedChangeListener(mChangeRadio);
}

private RadioGroup.OnCheckedChangeListener mChangeRadio = new
RadioGroup.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
// TODO Auto-generated method stub
if(checkedId==mRadio1.getId())
{
/*获取mRadio1的内容*/
result= mRadio1.getText().toString();
mTextView1.setText(result);
}
else if(checkedId==mRadio2.getId())
{
/*获取mRadio2的内容*/
result= mRadio2.getText().toString();
mTextView1.setText(mRadio2.getText());
}
}
};
}