Android 未调用单选按钮OnCheckChanged
从我的Android 未调用单选按钮OnCheckChanged,android,expandablelistview,radio-group,Android,Expandablelistview,Radio Group,从我的MainView(ExpandableListView)中,我在RadioButton上定义了child\u row.xml,我设置了监听器,但是OnCheckChanged不会被调用。如果我将RadioButton移动到currentview,它确实会被调用 我怎样才能解决这个问题 我有 mainListView.xml//expandableListView group_row.xml//textView child_row.xml//具有自定义单选按钮 → 您没有提供您使用的完整代码
MainView
(ExpandableListView
)中,我在RadioButton
上定义了child\u row.xml
,我设置了监听器,但是OnCheckChanged
不会被调用。如果我将RadioButton
移动到currentview,它确实会被调用
我怎样才能解决这个问题我有
您没有提供您使用的完整代码。如果以后将该膨胀的子视图添加到
活动的布局中,则应提供主布局,以查看是否以某种方式阻止了复选框。如果您只是膨胀布局,那么您不会得到checked事件是正常的,因为该子视图在屏幕上没有要执行操作的位置
从代码的外观来看,您似乎希望为ExpandableListView
子项中的复选框设置侦听器。如果这是您想要的,那么这是不正确的尝试,方法是为您的ExpandableListView
使用自定义适配器。关于构建自定义适配器和将侦听器设置为子元素,有很多教程(以及这里的问题)。必须调用它,可能会更改条件“if(group==answersGroup)”正如测试java中的两个对象是否相同一样,使用equals方法进行测试一样,当我从不同的布局获取ID时,缺少了一些东西——这导致它不调用该方法。就像我说的,如果我将收音机移动到currentView的布局,它会调用它。您的复选框在方法内-但该方法未被调用。是的,我希望侦听器在子对象内选中复选框。自定义适配器是正确的方法吗?在适配器中,实际的复选框侦听器代码在哪里?@user1529412是的,自定义适配器是设置侦听器的正确方法。在它的getChildView()
方法中,膨胀布局,然后设置复选框的侦听器。谢谢-效果很好。
public class Main_ListView extends Activity implements OnCheckedChangeListener{
public void onCreate(Bundle savedInstanceState) {
try{
super.onCreate(savedInstanceState);
setContentView(R.layout.mainListView);
ExpandableListView elv = (ExpandableListView) findViewById(R.id.expandableListView1);
SimpleExpandableListAdapter expListAdapter =
new SimpleExpandableListAdapter(
getApplicationContext(),
createGroupList(), // Creating group List.
R.layout.group_row, // Group item layout XML.
new String[] { "Group Item" }, // the key of group item.
new int[] { R.id.row_name }, // ID of each group item.-Data under the key goes into this TextView.
createChildList(), // childData describes second-level entries.
R.layout.child_row, // Layout for sub-level entries(second level).
new String[] {"Sub Item"}, // Keys in childData maps to display.
new int[] { R.id.grp_child} // Data under the keys above go into these TextViews.
);
elv.setAdapter(expListAdapter); // setting the adapter in the list.
}catch(Exception e){
System.out.println("Errrr +++ " + e.getMessage());
}
final LayoutInflater factory = getLayoutInflater();
final View childview = factory.inflate(R.layout.child_row, null);
answersGroup = (SegmentedRadioGroup) childview.findViewById(R.id.answersGroup);
if(answersGroup != null)
answersGroup.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (group == answersGroup) {
if (checkedId == R.id.radio0) {
mToast.setText("One");
mToast.show();
} else if (checkedId == R.id.radio1) {
mToast.setText("Two");
mToast.show();
} else if (checkedId == R.id.radio2) {
mToast.setText("Three");
mToast.show();
}
}
}