你好,我是一个学生,我的项目是制作一个应用程序的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));
}
好的,您的代码有一些问题:
首先,finalMemberTag始终为空,因为您从未初始化它。如果您希望使用字符串数组,并且希望将文本存储在复选框上,那么我们应该假设您最多将选中所有复选框(16)。这意味着您希望将数组初始化为:
专用字符串[]finalMemberTag=新字符串[16]
其次,我认为最好使用string集来避免重复,或者,如果需要特定索引的信息,使用ArrayList。
与设置:
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(index, buttonText); //this will add the text to the required index
} else {
// other stuff ...
finalMembersTag.remove(index); //this will remove the element at said index, or if you want an empty you can do finalMembersTag.add(index, "");
}
}