猿问

设置单选按钮值

RadioGroup genderGroup;

RadioButton genderType;

genderGroup = findViewById(R.id.radioGroup);

int id = genderGroup.getCheckedRadioButtonId();

genderType = findViewById(id);

如何为单选按钮组内的单选按钮设置值。性别类型获得一个值,但我不知道如何将单选按钮设置为选中状态


下面的代码是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>

我想要做的是保存一个选中的单选按钮,然后更新它或从数据库中检索它并显示它。


PIPIONE
浏览 132回答 3
3回答

慕虎7371278

在您获得RadioButton使用检查的 id后getCheckedRadioButtonId(),您可以将其与 2 个存在进行比较RadioButton并确定哪个是真正检查并保存到数据库:int genderValue = 0;if (id == R.id.radioButtonMale){&nbsp; &nbsp; genderValue = 0;}else{&nbsp; &nbsp; genderValue = 1;}//then you can save genderValue to database//get value from databaseint genderValue = ....; //get from databaseif (genderValue == 0){&nbsp; &nbsp; radioGroup.check(R.id.radioButtonMale);}else{&nbsp; &nbsp; radioGroup.check(R.id.radioButtonFemale);}

慕勒3428872

你可以switch在这样的事情中使用:&nbsp; &nbsp;RadioGroup&nbsp; mRadioGroup = findViewById(R.id.radioGroup);&nbsp; &nbsp;RadioButton mRadioBtnmale =&nbsp; findViewById(R.id.radioButtonMale);&nbsp; &nbsp;RadioButton mRadioBtnfemale =&nbsp; findViewById(R.id.radioButtonFemale);&nbsp; &nbsp;mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {&nbsp; &nbsp; &nbsp; &nbsp;@Override&nbsp; &nbsp; &nbsp; &nbsp;public void onCheckedChanged(RadioGroup group, int checkedId) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switch (checkedId){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case R.id.radioButtonMale:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(MainActivity.this,"Male selected",Toast.LENGTH_LONG).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case R.id.radioButtonFemale:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(MainActivity.this,"FeMale selected",Toast.LENGTH_LONG).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp;});如果您想显式设置值,您可以这样做,但是您的逻辑应该决定检查和取消选中哪一个:&nbsp; &nbsp;mRadioBtnmale.setChecked(true);&nbsp; &nbsp;mRadioBtnmale.setChecked(false);

MMTTMM

试试这个代码--- Main.xml&nbsp; &nbsp; &nbsp; &nbsp; <RadioGroup&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/radioSex"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content" >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <RadioButton&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/radioMale"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:text="@string/radio_male"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:checked="true" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <RadioButton&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/radioFemale"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:text="@string/radio_female" />&nbsp; &nbsp; &nbsp; &nbsp; </RadioGroup>活动.java&nbsp; &nbsp; &nbsp; &nbsp; private RadioGroup radioSexGroup;&nbsp; &nbsp; &nbsp; &nbsp; private RadioButton radioSexButton;&nbsp; &nbsp; &nbsp; &nbsp; private Button btnDisplay;&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.main);&nbsp; &nbsp; &nbsp; &nbsp; addListenerOnButton();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public void addListenerOnButton() {&nbsp; &nbsp; &nbsp; &nbsp; radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);&nbsp; &nbsp; &nbsp; &nbsp; btnDisplay = (Button) findViewById(R.id.btnDisplay);&nbsp; &nbsp; &nbsp; &nbsp; btnDisplay.setOnClickListener(new OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; // get selected radio button from radioGroup&nbsp; &nbsp; &nbsp; &nbsp; int selectedId = radioSexGroup.getCheckedRadioButtonId();&nbsp; &nbsp; &nbsp; &nbsp; // find the radiobutton by returned id&nbsp; &nbsp; &nbsp; &nbsp; radioSexButton = (RadioButton) findViewById(selectedId);&nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(MyAndroidAppActivity.this, radioSexButton.getText(), Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; }
随时随地看视频慕课网APP

相关分类

Java
我要回答