2つのEditTextボックスと2つのラジオボタンを含むRadioGroupがあります。いずれかの編集テキストボックスに入力を入力し始めると、すべてのラジオボタンが自動的にオフになり(ボタンのいずれかがすでにチェックされている場合)、別の編集テキストもクリアする必要があります。そして、2つのラジオボタンのいずれかをチェックすると、すべてのエディットテキストボックスがクリアされます(入力されたデータがある場合)。これが私がこれまでにしたことです。
oncreate()の内部:
et1 = (EditText)findViewById(R.id.editText1);
et1.setOnFocusChangeListener(this);
et1.addTextChangedListener(this);
et2 = (EditText)findViewById(R.id.editText2);
et2.setOnFocusChangeListener(this);
et2.addTextChangedListener(this);
radioButton1 = (RadioButton)findViewById(R.id.rb1);
radioButton2 = (RadioButton)findViewById(R.id.rb2);
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener(){
public void onCheckedChanged(RadioGroup group, int checkedId) {
et1.setText("");
et2.setText("");
}
});
と ...
@Override
public void onFocusChange(View v, boolean hasFocus) {
switch (v.getId()) {
case R.id.editText1:
whoHasFocus = 1;
break;
case R.id.editText2 :
whoHasFocus = 2;
break;
}
}
と
@Override
public void afterTextChanged(Editable edt) {
if(whoHasFocus == 1){
String enteredName1 = edt.toString().trim();
if(enteredName1.length() == 1){
if(et2.getText().toString().trim().length() >= 1)
et2.setText("");
radioButton1.setChecked(false);
radioButton2.setChecked(false);
}
}else if(whoHasFocus == 2){
String enteredName2 = edt.toString().trim();
if(enteredName2.length() == 1){
if(et1.getText().toString().trim().length() >= 1)
et1.setText("");
radioButton1.setChecked(false);
radioButton2.setChecked(false);
}
}
}
ラジオボタンをチェックして編集テキストの入力を開始すると、ラジオボタンのチェックが外れますが、初めて編集テキストに何も入力されません。次の文字を入力すると、2番目の文字が入力され、それ以降は正常に機能します。ただし、ラジオボタンがチェックされている場合、最初の文字はendittextに入力されていません。私を助けてください。どこが間違っているのですか?