我正在尝试在下图中准备自定义无线电组,如布局.我有将近8-10行.所以,我准备了一个linear layout
具有水平方向和添加imageview
,textview
和radiobutton
编程.
因此,如果我检查一个radio button
,其他单选按钮应自动取消选中.在进行该任务之前,我遇到了另一个问题,即如果我的单选按钮被选中一次,那么虽然点击它们但单选按钮仍然无法检查.以下是我的代码.
public class MainActivity extends Activity{ RadioButton[] radioBtns = new RadioButton[10]; String texts[] = {"text1", "text2", .... "text10"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); entireLayout = (LinearLayout)findViewById(R.id.main_layout); for(int i =0; i<10; i++) { LinearLayout rowLayout=new LinearLayout(this); radioBtns[i] = new RadioButton(this); radioBtns[i].setId(i); radioBtns[i].setOnCheckedChangeListener(cblistener); ImageView imageView = new ImageView(this); TextView tv = new TextView(this); tv.setText(texts[i]); rowLayout.addView(imageView); rowLayout.addView(tv); rowLayout.addView(radioBtns[i]); entireLayout.addView(rowLayout); View line = new View(this); line.setBackgroundColor(getResources().getColor(R.color.horizontallinecolor)); entireLayout.addView(line, new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, 1)); } } CompoundButton.OnCheckedChangeListener cblistener = new CompoundButton.OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton checkedbutton, boolean paramAnonymousBoolean) { switch (checkedbutton.getId()) { case 0: Log.d("tag", "checked 0th position"); break; .......................... .......................... } } }; }
我通过记录日志来观察.控件在第一次进入onCheckedChanged()时会进行检查,但是在未选中时则不会.我想知道这些是如何不可检查的.
或者 根据我的要求,我还有一个想法来准备这个布局,就像我为xml中的行准备一个更多的布局.然后膨胀视图10次.但是,如何只检查一个单选按钮,以便其他选定的单选按钮取消选中.有人可以建议我如何通过最好的方式实现这种放射组?
注意:我保留了上面的代码,因为它显示了我尝试过的内容.如果这是完全错误的方式,请放轻松,请建议我如何完成.