//首先需在MainActivity implements監聽動作
public class MainActivity extends AppCompatActivity
implements RadioGroup.OnCheckedChangeListener,TextWatcher
{
RadioGroup unit;
EditText value;
TextView txv;
@Overrideprotected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//設定要監聽的物件
unit=(RadioGroup)findViewById(R.id.radioGroup);
unit.setOnCheckedChangeListener(this);
value=(EditText)findViewById(R.id.editText);
value.addTextChangedListener(this);
txv=(TextView)findViewById(R.id.textView2);
}
//以下是監聽的事件
@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {
calc();
}
@Overridepublic void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Overridepublic void afterTextChanged(Editable s) {
calc();
}
protected void calc(){
}
}