[英]How to choose Radio Button on android studio
I want to make a radiobutton, which a radiobutton selected, a edit text is appears. 我要制作一个单选按钮,选中该单选按钮,将显示一个编辑文本。
For example, a have a 2 radio button 例如,有一个2单选按钮
A. two Edittext appears
B. three edittext appears
how i make this? 我怎么做的?
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.radio_pirates:
if (checked)
// Pirates are the best
break;
case R.id.radio_ninjas:
if (checked)
// Ninjas rule
break;
}
}
ready to 3 EditText 准备好3 EditText
EditText editTextA = null;
EditText editTextB = null;
EditText editTextC = null;
and when choose A button code: 当选择一个按钮代码时:
editTextA.setVisibility(View.VISIBLE);
editTextB.setVisibility(View.VISIBLE);
editTextC.setVisibility(View.GONE);
and when choose B button code: 当选择B按钮代码时:
editTextA.setVisibility(View.VISIBLE);
editTextB.setVisibility(View.VISIBLE);
editTextC.setVisibility(View.VISIBLE);
when when you choose A button it will just show editTextA,B and when you choose B, all 当您选择A按钮时,它只会显示editTextA,B,而当您选择B时,所有
use radioGroup
over radioButton
and than use setOnCheckedChangeListener for that radioGroup
and you will get checkedId in listener. 在
radioButton
使用radioGroup
, radioButton
对该radioGroup
使用setOnCheckedChangeListener,您将在侦听器中获得checkedId。
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged (RadioGroup group,int checkedId){
Log.d("chk", "id" + checkedId);
if (checkedId == R.id.a) {
//some code
} else if (checkedId == R.id.b) {
//some code
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.