簡體   English   中英

如何檢查選擇了哪個單選按鈕?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM