[英]Set Radio Button Value
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>
我想要做的是保存一個選中的單選按鈕,以后再對其進行更新或從數據庫中檢索並顯示它。
謝謝
在獲得通過使用getCheckedRadioButtonId()
檢查過的RadioButton
id之后,可以將其與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);
}
您可以在以下代碼中使用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);
試試這個代碼--- 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();
}
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.