AndroidのRadioButtonにOnClickListenerを設定するにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/8323778

質問

私は2つを持っています RadioButtons内部 RadioGroup. 。設定したい OnClickListener それらについて RadioButtons。どちらに依存します RadioButton クリックして、のテキストを変更したい EditText. 。どうすればこれを達成できますか?

役に立ちましたか?

解決

より良い方法は使用することだと思います RadioGroup そして、これについてリスナーを設定して変更して更新します View したがって、2つまたは3つまたは4などのリスナーを使用しています)。

    RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);        
    radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // checkedId is the RadioButton selected
        }
    });

他のヒント

これがあなたを助けることを願っています...

RadioButton rb = (RadioButton) findViewById(R.id.yourFirstRadioButton);
rb.setOnClickListener(first_radio_listener);

OnClickListener first_radio_listener = new OnClickListener (){
 public void onClick(View v) {
   //Your Implementaions...
 }
};
 radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
    {
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // checkedId is the RadioButton selected
            RadioButton rb=(RadioButton)findViewById(checkedId);
            textViewChoice.setText("You Selected " + rb.getText());
            //Toast.makeText(getApplicationContext(), rb.getText(), Toast.LENGTH_SHORT).show();
        }
    });

質問はについてでした 検出 どのラジオボタンがクリックされているか、これがクリックされたボタンを取得する方法です

final RadioGroup radio = (RadioGroup) dialog.findViewById(R.id.radioGroup1);
        radio.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                View radioButton = radio.findViewById(checkedId);
                int index = radio.indexOfChild(radioButton);

                // Add logic here

                switch (index) {
                case 0: // first button

                    Toast.makeText(getApplicationContext(), "Selected button number " + index, 500).show();
                    break;
                case 1: // secondbutton

                    Toast.makeText(getApplicationContext(), "Selected button number " + index, 500).show();
                    break;
                }
            }
        });

XMLレイアウトからリスナーを追加することもできます。 android:onClick="onRadioButtonClicked" あなたの中で <RadioButton/> 鬼ごっこ。

<RadioButton android:id="@+id/radio_pirates"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/pirates"
    android:onClick="onRadioButtonClicked"/>

見る Android開発者SDK-ラジオボタン 詳細については。

他の誰かが受け入れられている答えに苦労していた場合に備えて:

異なるoncheckedChangelistenerインターフェイスがあります。最初の1つに追加して、チェックボックスが変更されたかどうかを確認しました。

import android.widget.CompoundButton.OnCheckedChangeListener;

vs

import android.widget.RadioGroup.OnCheckedChangeListener;

リッキーからスニペットを追加するとき、私はエラーがありました:

タイプのradiogroupのsetoncheckedchangelistener(radiogroup.oncheckedchangelistener)のメソッドは、引数には適用されません(new compountton.oncheckedchangelistener(){})

Aliからの回答で修正できます:

new RadioGroup.OnCheckedChangeListener()

この質問はJavaに固有のものではないので、あなたがそれをどのように行うことができるかを追加したいと思います コトリン:

radio_group_id.setOnCheckedChangeListener({ radioGroup, optionId -> {
        when (optionId) {
            R.id.radio_button_1 -> {
                // do something when radio button 1 is selected
            }
            // add more cases here to handle other buttons in the RadioGroup
        }
    }
})

ここ radio_group_id 割り当てられています android:id 関係する放射層の。この方法で使用するには、インポートする必要があります kotlinx.android.synthetic.main.your_layout_name.* アクティビティのKotlinファイル。また、その場合に注意してください radioGroup lambdaパラメーターは未使用であり、に置き換えることができます _ (アンダースコア)コトリン1.1以来。

RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);     
    radioGroup.setOnClickListener(v -> {
                        // get selected radio button from radioGroup
                        int selectedId = radioGroup.getCheckedRadioButtonId();
                        // find the radiobutton by returned id
                        radioButton =  findViewById(selectedId);
                        String slectedValue=radioButton.getText()          
        });
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top