只有1/2的单选按钮在android中运行

时间:2012-04-01 22:13:30

标签: java android xml

这是我的XML,

            <RadioGroup
            android:id="@+id/radioType"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:orientation="horizontal"
            >

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

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

        </RadioGroup>

这是我的showAuctionOptions函数,

    public void showAuctionOptions(View v){

    switch(v.getId()){

    //execute this case if auction button is checked
        case R.id.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); 

            //Hide quantity
            LinearLayout HideQuantity = (LinearLayout)findViewById(R.id.LayoutQuantity);
            HideQuantity.setVisibility(View.GONE);
        break;

        //execute this case if buy it now button is checked
        case R.id.RadioBin:
            //Hide start price
            LinearLayout HidePrice = (LinearLayout)findViewById(R.id.LayoutStartPrice);
            HidePrice.setVisibility(View.GONE);

            //Hide reserve price
            LinearLayout HideReservePrice = (LinearLayout)findViewById(R.id.LayoutReservePrice);
            HideReservePrice.setVisibility(View.GONE);

            //Display quantity
            LinearLayout DisplayQuantity = (LinearLayout)findViewById(R.id.LayoutQuantity);
            DisplayQuantity.setVisibility(View.VISIBLE);
        break;

    }


}

我的问题是,它只显示按下拍卖单选按钮时的选项。按下现在购买时没有任何反应。有人可以帮忙吗?谢谢!

2 个答案:

答案 0 :(得分:1)

不要为RadioButtons使用'onClick'侦听器。请尝试使用RadioGroup.OnCheckedChangeListener。我认为您不能像使用android:onClick那样在XML布局文件中分配它。

Activity按如下方式实现界面......

public class MyActivity extends Activity
    implements RadioGroup.OnCheckedChangeListener {
    ...

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        ...
    }
}

然后,您需要找到RadioGroup并设置监听器...

RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioType);
radioGroup.setOnCheckedChangeListener (this);

答案 1 :(得分:0)

您是否为RadioBin设置了onclick侦听器?

RadioButton radioBin = (RadioButton) findViewById(R.id.RadioBin);
radioBin.setOnClickListener(...); 

就像你为RadioAuction做的那样?在我看来,RadioBin没有听众。