Java 设置单选按钮值
如何为单选按钮(位于单选组内)设置值。 genderType获取一个值,但我不知道如何将单选按钮设置为选中状态 下面的代码是xml文件中使用的代码Java 设置单选按钮值,java,android,Java,Android,如何为单选按钮(位于单选组内)设置值。 genderType获取一个值,但我不知道如何将单选按钮设置为选中状态 下面的代码是xml文件中使用的代码 RadioGroup genderGroup; RadioButton genderType; genderGroup = findViewById(R.id.radioGroup); int id = genderGroup.getCheckedRadioButtonId(); genderType = findViewById(id); 我
RadioGroup genderGroup;
RadioButton genderType;
genderGroup = findViewById(R.id.radioGroup);
int id = genderGroup.getCheckedRadioButtonId();
genderType = findViewById(id);
我试图做的是保存选中的单选按钮,然后更新它或从数据库中检索它并显示它
谢谢在您获得使用
getCheckedRadioButtonId()
检查的RadioButtonid
id后,您可以将其与2个现有的RadioButton
进行比较,并确定哪个是真正选中的并保存到数据库:
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="210dp"
android:layout_height="63dp"
android:layout_alignStart="@+id/editTextUsername"
android:layout_alignTop="@+id/textView6">
<RadioButton
android:id="@+id/radioButtonMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Male" />
<RadioButton
android:id="@+id/radioButtonFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Female" />
</RadioGroup>
您可以在如下情况下使用
开关
:
int genderValue = 0;
if (id == R.id.radioButtonMale){
genderValue = 0;
}else{
genderValue = 1;
}
//then you can save genderValue to database
//get value from database
int genderValue = ....; //get from database
if (genderValue == 0){
radioGroup.check(R.id.radioButtonMale);
}else{
radioGroup.check(R.id.radioButtonFemale);
}
如果要显式设置值,您可以这样做,但是您应该决定检查和取消检查哪一个:
RadioGroup mRadioGroup = findViewById(R.id.radioGroup);
RadioButton mRadioBtnmale = findViewById(R.id.radioButtonMale);
RadioButton mRadioBtnfemale = findViewById(R.id.radioButtonFemale);
mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.radioButtonMale:
Toast.makeText(MainActivity.this,"Male selected",Toast.LENGTH_LONG).show();
break;
case R.id.radioButtonFemale:
Toast.makeText(MainActivity.this,"FeMale selected",Toast.LENGTH_LONG).show();
}
}
});
请尝试以下代码--Main.xml
mRadioBtnmale.setChecked(true);
mRadioBtnmale.setChecked(false);
<RadioGroup
android:id="@+id/radioSex"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/radioMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_male"
android:checked="true" />
<RadioButton
android:id="@+id/radioFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_female" />
</RadioGroup>
private RadioGroup radioSexGroup;
private RadioButton radioSexButton;
private Button btnDisplay;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);
btnDisplay = (Button) findViewById(R.id.btnDisplay);
btnDisplay.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// get selected radio button from radioGroup
int selectedId = radioSexGroup.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioSexButton = (RadioButton) findViewById(selectedId);
Toast.makeText(MyAndroidAppActivity.this, radioSexButton.getText(), Toast.LENGTH_SHORT).show();
}
});
}