Android의 RadioButton에 OnClickListener를 설정하는 방법은 무엇입니까?
나는 두가 RadioButton
, 안쪽들 RadioGroup
. 나는 OnClickListener
그 RadioButton
s 를 설정하고 싶습니다 . 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-라디오 버튼 을 참조하세요.
Just in case someone else was struggeling with the accepted answer:
There are different OnCheckedChangeListener-Interfaces. I added to first one to see if a CheckBox was changed.
import android.widget.CompoundButton.OnCheckedChangeListener;
vs
import android.widget.RadioGroup.OnCheckedChangeListener;
When adding the snippet from Ricky I had errors:
The method setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener) in the type RadioGroup is not applicable for the arguments (new CompoundButton.OnCheckedChangeListener(){})
Can be fixed with answer from Ali :
new RadioGroup.OnCheckedChangeListener()
Since this question isn't specific to Java, I would like to add how you can do it in Kotlin:
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
}
}
})
Here radio_group_id
is the assigned android:id
of the concerned RadioGroup. To use it this way you would need to import kotlinx.android.synthetic.main.your_layout_name.*
in your activity's Kotlin file. Also note that in case the radioGroup
lambda parameter is unused, it can be replaced with _
(an underscore) since Kotlin 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()
});
참고URL : https://stackoverflow.com/questions/8323778/how-to-set-onclicklistener-on-a-radiobutton-in-android
'Programing' 카테고리의 다른 글
jQuery 및 CSS를 사용하여 숫자를 별표 평점 표시로 전환 (0) | 2020.08.19 |
---|---|
기본적으로 tinymce 붙여 넣기를 일반 텍스트로 만드는 방법 (0) | 2020.08.19 |
JavaScript에서 괄호로 함수를 호출하면 차이가 있습니까? (0) | 2020.08.19 |
이벤트를 한 번만 바인딩 (0) | 2020.08.19 |
.animate () 콜백이 두 번 호출됩니다. (0) | 2020.08.19 |