【发布时间】:2019-05-03 02:06:53
【问题描述】:
我正在尝试创建一个过滤列表的系统(在这种情况下,每个元素都是一个 RelativeLayout 元素)。我在我的 XML 代码中使用了 3 个 RadioButton 元素,其 ID 为 radio1、radio2、radio3,分别显示前 3、前 5 和前 10 个元素。在我的 java 类中,我试图使用 RadioButton.isChecked() 方法来创建列表,但由于某种原因,条件从未满足。有人可以帮帮我吗?
private void populateList() { //
RelativeLayout popList[] = new RelativeLayout[10];
RadioButton radio1 = (RadioButton) findViewById(R.id.radio1); //top 3
RadioButton radio2 = (RadioButton) findViewById(R.id.radio2); //top 5
RadioButton radio3 = (RadioButton) findViewById(R.id.radio3); //top 10
//each represents an elemnent of the list
popList[0] = (RelativeLayout) findViewById(R.id.populate_list1);
popList[1] = (RelativeLayout) findViewById(R.id.populate_list2);
popList[2] = (RelativeLayout) findViewById(R.id.populate_list3);
popList[3] = (RelativeLayout) findViewById(R.id.populate_list4);
popList[4] = (RelativeLayout) findViewById(R.id.populate_list5);
popList[5] = (RelativeLayout) findViewById(R.id.populate_list6);
popList[6] = (RelativeLayout) findViewById(R.id.populate_list7);
popList[7] = (RelativeLayout) findViewById(R.id.populate_list8);
popList[8] = (RelativeLayout) findViewById(R.id.populate_list9);
popList[9] = (RelativeLayout) findViewById(R.id.populate_list10);
//conditions
if(radio1.isChecked()) {
for(int i = 0; i < 3; i++) {
popList[i].setVisibility(View.VISIBLE);
}
for(int i = 3; i < 10; i++) {
popList[i].setVisibility(View.GONE);
}
}
else if(radio2.isChecked()) {
for(int i = 0; i < 5; i++) {
popList[i].setVisibility(View.VISIBLE);
}
for(int i = 5; i < 10; i++) {
popList[i].setVisibility(View.GONE);
}
}
else if(radio3.isChecked()) {
for(int i = 0; i < 10; i++) {
popList[i].setVisibility(View.VISIBLE);
}
}
}
【问题讨论】:
标签: java android xml android-studio radio-button