我有一個簡單的程序,在一個組中有3個單選按鈕。當我點擊任何單選按鈕時,我會看到一個Toast,讓用戶知道他們選擇了哪個按鈕。RadioButton清除檢查操作
我正面臨的問題是我有一個onClick
事件爲一個按鈕,該按鈕是無線電組clearCheck
。發生這種情況時,Toast消息將再次出現,並顯示從單選按鈕中選擇的先前信息。
當我收到廣播組時,我該如何防止發生吐司?
public class MainActivity extends AppCompatActivity {
RadioGroup radioGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize Radio Group and attach click handler
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioGroup.clearCheck();
// Attach CheckedChangeListener to Radio Group
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb = (RadioButton) group.findViewById(checkedId);
if(null!=rb && checkedId > -1){
Toast.makeText(MainActivity.this, rb.getText(), Toast.LENGTH_SHORT).show();
}
}
});
}
public void onClear(View v){
// Clears the radio buttons when the clear button is pressed
radioGroup.clearCheck();
}
顯示您的onClickListener? – dilix
OP沒有在類中使用任何監聽器,而是在xml中聲明它。 – Rohit5k2
嘗試寫入以記錄'clearCheck'方法之後checkId的值。 – dilix