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

时间:2012-04-01 20:56:14

标签: java android xml

尝试获取在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" />

所以一旦点击它就会运行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); 
    }
}

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

2 个答案:

答案 0 :(得分:0)

尝试

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); 
}

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

答案 1 :(得分:0)

可能会更快:

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); 
}

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

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