3

同じRadioGroup内の他のRadioButtonをクリックすると、以下の動的に生成されたRadioButtonのチェックを外すことができません。

RadioGroup (以下) をクリアする独自のハンドラーを作成し、すべての RadioButtons を .setChecked(false) にする別のハンドラーを作成しましたが、PopulateAccessPoints で setChecked した RadioButton はまだクリアされません。

何か案は?

RelativeLayout rlaAccessPoints;
RadioGroup rg;

public void onRadioButtonClicked(View view) {
// Is the button now checked?
RadioButton rd = (RadioButton)view;
rg.clearCheck();
rd.setChecked(true);
}


private void PopulateAccessPoints(List<clsAccessPoint> accessPoints){
rg = new RadioGroup(this);

for (clsAccessPoint acp :  accessPoints) {
    RadioButton rd = new RadioButton(this);
    rd.setText(acp.ID + ": " + acp.Name);

    rd.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            onRadioButtonClicked(v);
        }
    });

    rg.addView(rd);
}

rlaAccessPoints.addView(rg);

for (int i = 0; i <= rg.getChildCount() - 1; i++){
    RadioButton rd = new RadioButton(this);
    rd = (RadioButton)rg.getChildAt(i);

    String rdText = rd.getText().toString();
    int colonPos = rdText.indexOf(":");
    rdText = rdText.substring(0, colonPos).toString();
    if (Settings.AccessPointID.equals(rdText)){
        //rg.check(i);
        rd.setChecked(true);
    }
}

}

編集:より簡潔なコードで回答を以下に投稿しました。代わりにそれを見てください。

4

3 に答える 3

0

問題は、あなたが物事を二度やっているということです。他のユーザーの切り替えと選択解除を処理するラジオ グループが既にある場合。ラジオボタンを個別に操作しないでください。

RadioGroup のドキュメントを読み、適切なリスナーを実装してください

RadioButton rd = new RadioButton(this); rd = (ラジオボタン)rg.getChildAt(i);

これは意味がありません。最初にボタンを作成してから、RG の子に再割り当てします。

RadioButton rd = (RadioButton)rg.getChildAt(i);

正しいフォームである必要があります。

ラジオグループにこのリスナーを実装する必要があります: http://developer.android.com/reference/android/widget/RadioGroup.OnCheckedChangeListener.html

この部分を忘れて

 rd.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        onRadioButtonClicked(v);
    }
});
于 2014-08-20T14:15:01.460 に答える