android 动态获取复选框值
android Get Dynamically checkbox value
您好,我是一名学生,我的项目正在向 android 工作室提出申请。
我只是一个初学者所以一切都很困难:(
这是我的问题。
我根据当前动态创建了复选框。
如果用户单击 btnEx 按钮,我想获得 finalMemberTag。
但是在这段代码中,当我点击 btnEx 时,finalMemberTag 为空。
而且我希望如果用户单击复选框,则将复选框的文本添加到 finalMemberTag。
并且复选框未选中,将复选框的文本删除到 finalMemberTag。
我该怎么做?
(我已经设置了可绘制的东西。)
请帮助我...
private String[] finalMemberTag;
Button btnEx;
btnEx.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendData();
}
});
Switch(current){
case "A":
String[] memberA = {"all", "ab", "cd", "ef", "g", "hu", "hi", "dd"};
for (int i = 0; i < 8; i++) {
CheckBox chA = new CheckBox(getApplicationContext());
chA.setText(membersA[i]);
chA.setTypeface(ResourcesCompat.getFont(getApplicationContext(), R.font.nanumsquareround_r));
chA.setTextColor(Color.parseColor("#8D8D8D"));
cHA.setButtonDrawable(android.R.color.transparent);
chA.setBackground(inactiveCb);
int index = i;
chA.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
activeCb.setColorFilter(new PorterDuffColorFilter(starColor, PorterDuff.Mode.SRC_IN));
chA.setBackground(activeCb);
chA.setTextColor(startextColor);
finalMemberTag[index] = buttonView.getText().toString();
}
if(!isChecked) {
chA.setBackground(inactiveCb);
chA.setTextColor(Color.parseColor("#8D8D8D")); }
}
});
chA.setPadding(36, 24, 36, 24);
chA.setLayoutParams(params);
placeInputMember.addView(chA);
}
break;
case "B":
String[] memberB = {"all", "abc", "Dcd", "e3f", "DSg", "DGu", "hE", "dV"};
for (int i = 0; i < 8; i++) {
CheckBox chB = new CheckBox(getApplicationContext());
chB.setText(memberB[i]);
chB.setTypeface(ResourcesCompat.getFont(getApplicationContext(), R.font.nanumsquareround_r));
chB.setTextColor(Color.parseColor("#8D8D8D"));
chB.setButtonDrawable(android.R.color.transparent);
chB.setBackground(inactiveCb);
int index = i;
chB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
activeCb.setColorFilter(new PorterDuffColorFilter(starColor, PorterDuff.Mode.SRC_IN));
chB.setBackground(activeCb);
chB.setTextColor(startextColor);
finalMemberTag[index] = buttonView.getText().toString();
}
if(!isChecked) {
chB.setBackground(inactiveCb);
chB.setTextColor(Color.parseColor("#8D8D8D")); }
}
});
chB.setPadding(36, 24, 36, 24);
chB.setLayoutParams(params);
placeInputMember.addView(chB);
}
break;
}
public void sendData(){
Log.d("##", String.valueOf(finalMemberTag));
}
另一个错误
String[] membersA = {"all", "ji", "ha", "bo", "re", "co", "zo", "pi"};
arrayMember = new ArrayList<>();
for (int i = 0; i < membersBts.length; i++) {
CheckBox chA = new CheckBox(getApplicationContext());
chA .setText(members[i]);
chA.setTypeface(ResourcesCompat.getFont(getApplicationContext(), R.font.nanumsquareround_r));
chA.setTextColor(Color.parseColor("#8D8D8D"));
chA .setButtonDrawable(android.R.color.transparent);
chA.setBackground(inactiveCb);
int index = i;
chA.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
activeCb.setColorFilter(new PorterDuffColorFilter(starColor, PorterDuff.Mode.SRC_IN));
chA.setBackground(activeCb);
chA.setTextColor(startextColor);
arrayMember.add(index, buttonView.getText().toString());
}
if(!isChecked) {
chA.setBackground(inactiveCb);
chA.setTextColor(Color.parseColor("#8D8D8D"));
arrayMember.remove(index);
}
}
});
chA.setPadding(36, 24, 36, 24);
chA.setLayoutParams(params);
placeInputMember.addView(chA);
}
break;
错误LOGCAT
java.lang.IndexOutOfBoundsException: Index: 5, Size: 2
好的,您的代码有一些问题:
首先,finalMemberTag 始终为空,因为您从未对其进行初始化。如果您想使用字符串数组并且想将文本存储在复选框上,那么我们应该假设最多您将选中所有复选框 (16)。这意味着您需要将数组初始化为:
私有字符串[] finalMemberTag = 新字符串[16]
其次,我认为您最好使用字符串集来避免重复,或者,如果您需要有关特定索引的信息,则使用数组列表。
搭配套装:
private Set<String> finalMembersTag = new HashSet<>(16);
//onCreate...other stuff...for loop
//...setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
final String buttonText = buttonView.getText().toString();
if (isChecked) {
// other stuff ...
finalMembersTag.add(buttonText);
} else {
// other stuff ...
finalMembersTag.remove(buttonText);
}
}
使用 ArrayList:
private List<String> finalMembersTag = new ArrayList<>(16);
//onCreate...other stuff...for loop
int index = i;
chA.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
final String buttonText = buttonView.getText().toString();
if (isChecked) {
// other stuff ...
finalMembersTag.add(buttonText); //this will add the text
} else {
// other stuff ...
finalMembersTag.remove(buttonText); //this will remove the element if found, or if you want an empty string you can do finalMembersTag.add(index, "");
}
}
您好,我是一名学生,我的项目正在向 android 工作室提出申请。
我只是一个初学者所以一切都很困难:(
这是我的问题。
我根据当前动态创建了复选框。
如果用户单击 btnEx 按钮,我想获得 finalMemberTag。
但是在这段代码中,当我点击 btnEx 时,finalMemberTag 为空。
而且我希望如果用户单击复选框,则将复选框的文本添加到 finalMemberTag。 并且复选框未选中,将复选框的文本删除到 finalMemberTag。
我该怎么做? (我已经设置了可绘制的东西。)
请帮助我...
private String[] finalMemberTag;
Button btnEx;
btnEx.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendData();
}
});
Switch(current){
case "A":
String[] memberA = {"all", "ab", "cd", "ef", "g", "hu", "hi", "dd"};
for (int i = 0; i < 8; i++) {
CheckBox chA = new CheckBox(getApplicationContext());
chA.setText(membersA[i]);
chA.setTypeface(ResourcesCompat.getFont(getApplicationContext(), R.font.nanumsquareround_r));
chA.setTextColor(Color.parseColor("#8D8D8D"));
cHA.setButtonDrawable(android.R.color.transparent);
chA.setBackground(inactiveCb);
int index = i;
chA.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
activeCb.setColorFilter(new PorterDuffColorFilter(starColor, PorterDuff.Mode.SRC_IN));
chA.setBackground(activeCb);
chA.setTextColor(startextColor);
finalMemberTag[index] = buttonView.getText().toString();
}
if(!isChecked) {
chA.setBackground(inactiveCb);
chA.setTextColor(Color.parseColor("#8D8D8D")); }
}
});
chA.setPadding(36, 24, 36, 24);
chA.setLayoutParams(params);
placeInputMember.addView(chA);
}
break;
case "B":
String[] memberB = {"all", "abc", "Dcd", "e3f", "DSg", "DGu", "hE", "dV"};
for (int i = 0; i < 8; i++) {
CheckBox chB = new CheckBox(getApplicationContext());
chB.setText(memberB[i]);
chB.setTypeface(ResourcesCompat.getFont(getApplicationContext(), R.font.nanumsquareround_r));
chB.setTextColor(Color.parseColor("#8D8D8D"));
chB.setButtonDrawable(android.R.color.transparent);
chB.setBackground(inactiveCb);
int index = i;
chB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
activeCb.setColorFilter(new PorterDuffColorFilter(starColor, PorterDuff.Mode.SRC_IN));
chB.setBackground(activeCb);
chB.setTextColor(startextColor);
finalMemberTag[index] = buttonView.getText().toString();
}
if(!isChecked) {
chB.setBackground(inactiveCb);
chB.setTextColor(Color.parseColor("#8D8D8D")); }
}
});
chB.setPadding(36, 24, 36, 24);
chB.setLayoutParams(params);
placeInputMember.addView(chB);
}
break;
}
public void sendData(){
Log.d("##", String.valueOf(finalMemberTag));
}
另一个错误
String[] membersA = {"all", "ji", "ha", "bo", "re", "co", "zo", "pi"};
arrayMember = new ArrayList<>();
for (int i = 0; i < membersBts.length; i++) {
CheckBox chA = new CheckBox(getApplicationContext());
chA .setText(members[i]);
chA.setTypeface(ResourcesCompat.getFont(getApplicationContext(), R.font.nanumsquareround_r));
chA.setTextColor(Color.parseColor("#8D8D8D"));
chA .setButtonDrawable(android.R.color.transparent);
chA.setBackground(inactiveCb);
int index = i;
chA.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
activeCb.setColorFilter(new PorterDuffColorFilter(starColor, PorterDuff.Mode.SRC_IN));
chA.setBackground(activeCb);
chA.setTextColor(startextColor);
arrayMember.add(index, buttonView.getText().toString());
}
if(!isChecked) {
chA.setBackground(inactiveCb);
chA.setTextColor(Color.parseColor("#8D8D8D"));
arrayMember.remove(index);
}
}
});
chA.setPadding(36, 24, 36, 24);
chA.setLayoutParams(params);
placeInputMember.addView(chA);
}
break;
错误LOGCAT
java.lang.IndexOutOfBoundsException: Index: 5, Size: 2
好的,您的代码有一些问题:
首先,finalMemberTag 始终为空,因为您从未对其进行初始化。如果您想使用字符串数组并且想将文本存储在复选框上,那么我们应该假设最多您将选中所有复选框 (16)。这意味着您需要将数组初始化为:
私有字符串[] finalMemberTag = 新字符串[16]
其次,我认为您最好使用字符串集来避免重复,或者,如果您需要有关特定索引的信息,则使用数组列表。
搭配套装:
private Set<String> finalMembersTag = new HashSet<>(16);
//onCreate...other stuff...for loop
//...setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
final String buttonText = buttonView.getText().toString();
if (isChecked) {
// other stuff ...
finalMembersTag.add(buttonText);
} else {
// other stuff ...
finalMembersTag.remove(buttonText);
}
}
使用 ArrayList:
private List<String> finalMembersTag = new ArrayList<>(16);
//onCreate...other stuff...for loop
int index = i;
chA.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
final String buttonText = buttonView.getText().toString();
if (isChecked) {
// other stuff ...
finalMembersTag.add(buttonText); //this will add the text
} else {
// other stuff ...
finalMembersTag.remove(buttonText); //this will remove the element if found, or if you want an empty string you can do finalMembersTag.add(index, "");
}
}