获取android中单击的单选按钮的ID

时间:2021-06-25 21:21:13

Trying to get the id of the radio button that is checked in android, this is my XML code,

试图获取在android中检查的单选按钮的id,这是我的XML代码,

                <RadioButton
                android:id="@+id/RadioAuction"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="55dp"
                android:onClick="showAuctionOptions"
                android:textColor="#3DCC00"
                android:text="@string/RadioButton1" />

            <RadioButton
                android:id="@+id/RadioBin"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:textColor="#FF0000"
                android:text="Buy it now" />

so once it has been clicked it runs showAuctionOptions, this is my java code,

所以一旦点击它就会运行showAuctionOptions,这是我的java代码,

        public void showAuctionOptions(View v){


    if(findViewById(R.id.v=="RadioAuction")){
    //Display start price
    LinearLayout ShowPrice = (LinearLayout)findViewById(R.id.LayoutStartPrice);
    ShowPrice.setVisibility(View.VISIBLE);

    //Display reserve price
    LinearLayout ShowReservePrice = (LinearLayout)findViewById(R.id.LayoutReservePrice);
    ShowReservePrice.setVisibility(View.VISIBLE); 
    }
}

However this doesn't work, does anyone know why? Thanks.

然而,这不起作用,有谁知道为什么?谢谢。

2 个解决方案

#1


0  

Try

if (v == findViewById(R.id.RadioAuction)){
LinearLayout ShowPrice = (LinearLayout)findViewById(R.id.LayoutStartPrice);
    ShowPrice.setVisibility(View.VISIBLE);

    //Display reserve price
    LinearLayout ShowReservePrice = (LinearLayout)findViewById(R.id.LayoutReservePrice);
    ShowReservePrice.setVisibility(View.VISIBLE); 
}

If this doesn't cut it either, make sure you've set the on click listeners for those radio buttons.

如果这也没有删除它,请确保为这些单选按钮设置了单击侦听器。

#2


0  

Probably faster would be:

可能会更快:

if (v.getId() == R.id.RadioAuction){
  LinearLayout ShowPrice = (LinearLayout)findViewById(R.id.LayoutStartPrice);
  ShowPrice.setVisibility(View.VISIBLE);

  //Display reserve price
  LinearLayout ShowReservePrice = (LinearLayout)findViewById(R.id.LayoutReservePrice);
  ShowReservePrice.setVisibility(View.VISIBLE); 
}

And that way you can use the R values in a switch statement, if you have a long radio list:

这样,如果你有一个长的无线电列表,你可以在switch语句中使用R值:

switch (v.getId()) {
  case R.id.RadioAuction:
    // Do stuff
   ...
}

#1


0  

Try

if (v == findViewById(R.id.RadioAuction)){
LinearLayout ShowPrice = (LinearLayout)findViewById(R.id.LayoutStartPrice);
    ShowPrice.setVisibility(View.VISIBLE);

    //Display reserve price
    LinearLayout ShowReservePrice = (LinearLayout)findViewById(R.id.LayoutReservePrice);
    ShowReservePrice.setVisibility(View.VISIBLE); 
}

If this doesn't cut it either, make sure you've set the on click listeners for those radio buttons.

如果这也没有删除它,请确保为这些单选按钮设置了单击侦听器。

#2


0  

Probably faster would be:

可能会更快:

if (v.getId() == R.id.RadioAuction){
  LinearLayout ShowPrice = (LinearLayout)findViewById(R.id.LayoutStartPrice);
  ShowPrice.setVisibility(View.VISIBLE);

  //Display reserve price
  LinearLayout ShowReservePrice = (LinearLayout)findViewById(R.id.LayoutReservePrice);
  ShowReservePrice.setVisibility(View.VISIBLE); 
}

And that way you can use the R values in a switch statement, if you have a long radio list:

这样,如果你有一个长的无线电列表,你可以在switch语句中使用R值:

switch (v.getId()) {
  case R.id.RadioAuction:
    // Do stuff
   ...
}