【问题标题】:How to change Radio Button text into int?如何将单选按钮文本更改为 int?
【发布时间】:2012-11-07 04:03:45
【问题描述】:

我有一个广播组,有 2 个选项,即男性和女性。 我想将 RadioButton Male 作为 int number "1" 和 Female 作为 "2" 保存到我的数据库中。 但我真的不知道如何实现它。 如果有可能有人可以在这个问题上启发我吗? 提前谢谢你。

    <RadioGroup
    android:id="@+id/radio_sex"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/email_address"
    android:layout_alignTop="@+id/textView5"
    android:orientation="vertical" >

    <RadioButton
        android:id="@+id/radio_male"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Male" />

    <RadioButton
        android:id="@+id/radio_female"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Female" />
</RadioGroup>

【问题讨论】:

  • 您是否考虑过获取 RadioGroup 中 RadioButtons 的索引?如果没有,也许你需要看看这个question
  • 感谢 Lokesh Mehra,我在该问题中得到了一些非常有用的信息。

标签: android radio-button radio-group


【解决方案1】:
RadioGroup rg = (RadioGroup) findViewById(R.id.radio_sex); 
int selected = rg.getCheckedRadioButtonId();
RadioButton rb = (RadioButton) findViewById(selected);
if(rb.getText().equals("Male"){
  //save to db number one
}else{
  //save number two
}

【讨论】:

    【解决方案2】:

    将radioButton Male 的标签设置为1,将female 的标签设置为2。

    <RadioButton
        android:id="@+id/radio_male"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:tag="1"
        android:text="Male" />
    
     <RadioButton
        android:id="@+id/radio_female"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:tag="2"
        android:text="Female" />
    

    然后在检查更改时,获取所选单选按钮的标签并将其转换为 int 然后保存到数据库。

     setOnCheckedChangeListener(new OnCheckedChangeListener() {
    
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int value=Integer.parseInt(findViewById(checkedId).getTag().toString());
            }
        });
    

    【讨论】:

    • 我已经设法解决了我的问题,但仍然感谢您的回答。这为我提供了另一种方法来解决我的问题。
    【解决方案3】:
    <RadioGroup
        android:id="@+id/radio_sex"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/email_address"
        android:layout_alignTop="@+id/textView5"
        android:orientation="vertical" >
    
        <RadioButton
            android:id="@+id/radio_male"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Male" />
    
        <RadioButton
            android:id="@+id/radio_female"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Female" />
    </RadioGroup>
    

    现在在您的 java 文件中检查任一单选按钮

    int maleFemaleVar = ( radio_male.isChecked() ? 1 : 2 );
    

    现在将这个新的 maleFemaleVar 保存到您的数据库中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-16
      • 1970-01-01
      • 2014-04-09
      • 2015-11-24
      • 1970-01-01
      • 2015-08-06
      • 2018-07-21
      相关资源
      最近更新 更多