设置单选按钮值

时间:2018-10-14 16:35:04

标签: java android

RadioGroup genderGroup;
RadioButton genderType;
genderGroup = findViewById(R.id.radioGroup);
int id = genderGroup.getCheckedRadioButtonId();
genderType = findViewById(id);

如何为单选按钮设置单选按钮。 sexType获取值,但是我不知道如何将单选按钮设置为选中状态

下面的代码是xml文件中使用的代码

<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>

我想做的是保存一个选中的单选按钮,以后再对其进行更新或从数据库中检索并显示它。

谢谢

3 个答案:

答案 0 :(得分:0)

获得RadioButton的ID(使用getCheckedRadioButtonId()对其进行检查后),可以将其与2个存在的RadioButton进行比较,并确定哪个是真实检查并保存到数据库中:

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);
}

答案 1 :(得分:0)

您可以在以下情况下使用switch

   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();
            }
       }
   });

如果要显式设置值,可以这样做,但是逻辑上应确定要检查和不检查哪个值:

   mRadioBtnmale.setChecked(true);
   mRadioBtnmale.setChecked(false);

答案 2 :(得分:-1)

  

尝试使用此代码--- Main.xml

        <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>
  

Activity.java

        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();
        }
        });
        }