1

4 つのトグルボタンを持つラジオグループを実装しようとしました。望ましい動作は、典型的なラジオ グループの動作です。1 つのボタンが事前に選択され、ユーザーが別のボタンをクリックすると、前のボタンが選択解除され、代わりに新しいボタンが選択されます。ユーザーが選択したボタンをもう一度クリックしても、ボタンを選択することは許可されていないため、何も起こりません。そして、それが私が問題を抱えた場所です。この質問の解決策に従いました: Android: トグルボタンでラジオグループを取得するには?

残念ながら、ユーザーはまだアクティブなボタンを選択解除できます。どうすればこれを防ぐことができますか?

私のコードは次のとおりです。

ToggleButtons の onClick リスナー:

/**
 * Handler for onClick Events.
 */
    @Override
public void onClick(View v) {
    if (viewListener == null) {
        return;
    }
    if (v == tb_one|| v == tb_two|| v == tb_three|| v == tb_four) {
        ((RadioGroup) v.getParent()).check(v.getId());
    }
    else {
        super.onClick(v);
    }

}

私のカスタム OnCheckedChangeListener:

/**
 * The listener for a checked change event of the toggle buttons.
 */
static final RadioGroup.OnCheckedChangeListener ToggleListener = new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(final RadioGroup radioGroup, final int i) {
        //if one button is checked, uncheck all others
        for (int j = 0; j < radioGroup.getChildCount(); j++) {
            final ToggleButton view = (ToggleButton) radioGroup.getChildAt(j);
            view.setChecked(view.getId() == i);
        }
    }
};

そして、リスナーを追加する場所は次のとおりです(onFinishInflateメソッドにあります)

    ((RadioGroup) findViewById(R.id.instant_toggleGroup_mode))
            .setOnCheckedChangeListener(ToggleListener);
    tb_one = (ToggleButton) findViewById(R.id.instant_tb_one);
    tb_one.setOnClickListener(this);
    tb_two = (ToggleButton) findViewById(R.id.instant_tb_two);
    tb_two.setOnClickListener(this);
    tb_three = (ToggleButton) findViewById(R.id.instant_tb_three);
    tb_three.setOnClickListener(this);
    tb_four = (ToggleButton) findViewById(R.id.instant_tb_four);
    tb_four.setOnClickListener(this);

誰かが私に解決策を指摘できれば素晴らしいでしょう!

4

2 に答える 2

2

Finally I figured out how to do it. Thanks Ole, your help led me to this solution.

So this is the working code:

Initializing the buttons and the button group:

tb_one = (ToggleButton) findViewById(R.id.instant_tb_one);
tb_one.setOnClickListener(this);
tb_two = (ToggleButton) findViewById(R.id.instant_tb_two);
tb_two.setOnClickListener(this);
tb_three = (ToggleButton) findViewById(R.id.instant_tb_three);
tb_three.setOnClickListener(this);
tb_four = (ToggleButton) findViewById(R.id.instant_tb_four);
tb_four.setOnClickListener(this);
rg_modes = (RadioGroup) findViewById(R.id.instant_toggleGroup_mode);
rg_modes.setOnCheckedChangeListener(ToggleListener);
rg_modes.clearCheck();
rg_modes.check(tb_one.getId());

The onClick Handler:

if (v == tb_one|| v == tb_two|| v == tb_three|| v == tb_four) {
  rg_modes.clearCheck();
  rg_modes.check(v.getId());
}
于 2012-12-11T12:21:28.923 に答える
0

ToggleButton が既にチェックされているかどうかを確認します。そうであれば、何もしません。

if (v == tb_one|| v == tb_two|| v == tb_three|| v == tb_four) {
    if(!((ToggleButton) v).isChecked())
        ((RadioGroup) v.getParent()).check(v.getId());
}

編集

if (v == tb_one|| v == tb_two|| v == tb_three|| v == tb_four) {
    ((RadioGroup) v.getParent()).check(v.getId());

    if(!((ToggleButton) v).isChecked())
        ((ToggleButton) v).setChecked(true);
}
于 2012-12-10T14:33:28.083 に答える