2010年3月3日水曜日

RadioButtonを変更した時にRadioButtonのテキストを取得する。ボタンを押した時にEditTextのテキストを設定したRadioButtonを追加する。










・・省略・・・
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

//ラジオボタンを追加するボタン。エディットテキストのテキストを設定。
Button btCreate = (Button) findViewById(R.id.btcreate);
btCreate.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText edit = (EditText) findViewById(R.id.edit);
radioGroup = (RadioGroup) findViewById(R.id.radiogroup);
RadioButton newRadioButton = new RadioButton(TestTestActivity.this);
newRadioButton.setText(edit.getText().toString());
radioGroup.addView(newRadioButton, radioGroup.getChildCount());
newRadioButton.setChecked(true);
}
});



//RadioButtonのチェックが変更された時。EditTextの内容を変更。
radioGroup = (RadioGroup) findViewById(R.id.radiogroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
EditText edit = (EditText) findViewById(R.id.edit);
radioGroup = (RadioGroup) findViewById(R.id.radiogroup);
RadioButton selectedButton = (RadioButton) findViewById(radioGroup.getCheckedRadioButtonId());
edit.setText(selectedButton.getText());
}
});

}
}





0 件のコメント:

コメントを投稿