Dan*_*unt 4 android android-chips
我是在 Android 中使用 Chips 的新手。我想在单击按钮时从 ChipGroup 中获取选定的筹码。Made 以某种方式检查每个 Chip 并将其添加到 Set 中,但希望使其更有效率。不知何故,我自己没有找到我的问题的答案。
当我检查 2 Chips 并取消选中我检查的第一个 Chips 时,也会出现错误。
这是我的代码:
<com.google.android.material.chip.ChipGroup
android:id="@+id/chipGroup"
android:layout_width="300dp"
android:layout_height="wrap_content"
>
<com.google.android.material.chip.Chip
android:id="@+id/chip1"
style="@style/Widget.MaterialComponents.Chip.Filter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:backgroundTint="#99FFFFFF"
/>
<com.google.android.material.chip.Chip
android:id="@+id/chip2"
style="@style/Widget.MaterialComponents.Chip.Filter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:backgroundTint="#99FFFFFF"/>
<com.google.android.material.chip.Chip
android:id="@+id/chip3"
style="@style/Widget.MaterialComponents.Chip.Filter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:backgroundTint="#99FFFFFF"/>
<com.google.android.material.chip.Chip
android:id="@+id/chip4"
style="@style/Widget.MaterialComponents.Chip.Filter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:backgroundTint="#99FFFFFF"/>
<com.google.android.material.chip.Chip
android:id="@+id/chip5"
style="@style/Widget.MaterialComponents.Chip.Filter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
android:backgroundTint="#99FFFFFF"/>
</com.google.android.material.chip.ChipGroup>
Run Code Online (Sandbox Code Playgroud)
Set<Integer> chipIds = new HashSet<>();
int chip1Id= 1;
int chip2Id= 2;
int chip3Id= 3;
int chip4Id= 4;
int chip5Id= 5;
chip1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
chipIds.add(chip1Id);
} else {
for (int i : chipIds) {
if (i == chip1Id) {
chipIds.remove(i);
}
}
}
}
});
chip2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
chipIds.add(chip2Id);
} else {
for (int i : chipIds) {
if (i == chip2Id) {
chipIds.remove(i);
}
}
}
}
});
chip3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
chipIds.add(chip3Id);
} else {
for (int i : chipIds) {
if (i == chip3Id) {
chipIds.remove(i);
}
}
}
}
});
chip4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
chipIds.add(chip4Id);
} else {
for (int i : chipIds) {
if (i == chip4Id) {
chipIds.remove(i);
}
}
}
}
});
chip5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
chipIds.add(chip5Id);
} else {
for (int i : chipIds) {
if (i == chip5Id) {
chipIds.remove(i);
}
}
}
}
});
Run Code Online (Sandbox Code Playgroud)
对于那些寻找Kotlin等效版本的人来说,它看起来要简单得多。这个版本Sequence也使用了同样的方法,因此它比通常的 Java 版本更高效:
val chipGroup = // get the ChipGroup view via your favorite inflation mechanism (view bindind, android extensions, etc)
val selectedChips = chipGroup.children
.filter { (it as Chip).isChecked }
.map { (it as Chip).text.toString() }
Run Code Online (Sandbox Code Playgroud)
小智 5
这是我的解决方案:
<Button
android:id="@+id/bShow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/chipGroup"
android:layout_margin="10dp"
android:text="Show Checked"
android:onClick="buttonPressed"/>
Run Code Online (Sandbox Code Playgroud)
import android.widget.Button;
import android.widget.Toast;
import android.view.View;
Run Code Online (Sandbox Code Playgroud)
showResult = (Button) findViewById(R.id.bShow);
showResult.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
String msg = "Chips checked are:";
ChipGroup chg = findViewById(R.id.chipGroup);
int chipsCount = chg.getChildCount();
if (chipsCount == 0) {
msg += " None!!";
} else {
int i = 0;
while (i < chipsCount) {
Chip chip = (Chip) chg.getChildAt(i);
if (chip.isChecked() ) {
msg += chip.getText().toString() + " " ;
}
i++;
};
}
// show message
Toast.makeText(getApplicationContext(),msg, Toast.LENGTH_LONG).show();
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6959 次 |
| 最近记录: |