我有以下代码:
private void inflateCustomView()
{
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View customNav = inflater.inflate(R.layout.custom_view, null);
//Bind to its state change
RadioGroup radioGrp = ((RadioGroup)customNav.findViewById(R.id.radio_nav));
radioGrp.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
switch(checkedId)
{
case R.id.moneyRadioBtn:
isInCashMode = true;
break;
case R.id.unitRadioBtn:
isInCashMode = false;
break;
}
setCashOrUnits(isInCashMode);
}
});
//Attach to the action bar
getSupportActionBar().setCustomView(customNav);
getSupportActionBar().setDisplayShowCustomEnabled(true);
}
自定义视图似乎已正确添加,但从未调用过 onCheckedChanged。我尝试在 onCheckedChanged 中插入断点,但从未达到。我究竟做错了什么?
编辑:这是 custom_view 的 XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="left|center_vertical"
android:orientation="horizontal"
>
<RadioGroup
android:id="@+id/radio_nav"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<RadioButton
android:id="@+id/moneyRadioBtn"
android:text="Kr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
/>
<RadioButton
android:id="@+id/unitRadioBtn"
android:text="Enheder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
/>
</RadioGroup>
</LinearLayout>