![](/img/trans.png)
[英]How to check the radio button in RadioGroup in ListView Android
[英]How to check which radiogroup button is selected?
我在一個單選組中有三個單選按鈕。 我怎樣才能告訴 Java 根據選擇的按鈕做不同的事情? 我聲明了組和所有按鈕:
final RadioGroup size = (RadioGroup)findViewById(R.id.RGSize);
final RadioButton small = (RadioButton)findViewById(R.id.RBS);
final RadioButton medium = (RadioButton)findViewById(R.id.RBM);
final RadioButton large = (RadioButton)findViewById(R.id.RBL);
我知道我會這樣說:
if (size.getCheckedRadioButtonId().equals(small){
} else{
}
但是 equals 不是正確的語法...我怎么能問 java 選擇了哪個按鈕?
嘗試:
if (size.getCheckedRadioButtonId() == small.getId()){
....
}
else if(size.getCheckedRadioButtonId() == medium.getId()){
....
}
因為 getCheckedRadioButtonId getCheckedRadioButtonId()
返回 integer,所以您正在嘗試將 integer 與 RadioButton object 進行比較。您應該比較small
的 id(即R.id.RBS
)和 getCheckedRadioButtonId getCheckedRadioButtonId()
:
switch(size.getCheckedRadioButtonId()){
case R.id.RBS: //your code goes here..
break;
case R.id.RBM: //your code goes here..
break;
case R.id.RBL: //your code goes here..
break;
}
int selected = size.getCheckedRadioButtonId();
switch(selected){
case R.id.RBS:
break;
case R.id.RBM:
break;
case R.id.RBL:
break;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.