我有更多的芯片,我已经添加到数组,但如何显示它?



我有更多的芯片,如何添加到数组和使用?

我已经添加到数组,但如何显示它?

这是我的代码

String[] CampSite = new String[]{getApplicationContext().getResources().getString(R.string.campsite_wifi),
getApplicationContext().getResources().getString(R.string.campsite_wheelchair),
getApplicationContext().getResources().getString(R.string.campsite_water_from_tap),
getApplicationContext().getResources().getString(R.string.campsite_toilet),
getApplicationContext().getResources().getString(R.string.campsite_swimming_pool),
getApplicationContext().getResources().getString(R.string.campsite_surface),
getApplicationContext().getResources().getString(R.string.campsite_suit_any_car),
getApplicationContext().getResources().getString(R.string.campsite_pet_welcome),
getApplicationContext().getResources().getString(R.string.campsite_laundromat),
getApplicationContext().getResources().getString(R.string.campsite_large_vehicle_access),
getApplicationContext().getResources().getString(R.string.campsite_kitchen),
getApplicationContext().getResources().getString(R.string.campsite_internet),
getApplicationContext().getResources().getString(R.string.campsite_household_power),
getApplicationContext().getResources().getString(R.string.campsite_hot_shower),
getApplicationContext().getResources().getString(R.string.campsite_dump_station),
getApplicationContext().getResources().getString(R.string.campsite_credit_card),
getApplicationContext().getResources().getString(R.string.campsite_cellular_signal),
getApplicationContext().getResources().getString(R.string.campsite_caravan_power),
getApplicationContext().getResources().getString(R.string.campsite_cabin),
getApplicationContext().getResources().getString(R.string.campsite_bed_supplied),
getApplicationContext().getResources().getString(R.string.campsite_bbq),
};

这是xml

<com.google.android.material.chip.ChipGroup
android:theme="@style/CampsitePost"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

您需要循环列表CampSite并将它们添加到ChipGroup,在您的。xml

中设置id
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ChipGroup chipGroup = findViewById(R.id.chip_group_main);
for (String camp : CampSite)
{
Chip chip = new Chip(this);
chip.setText(camp);
chip.setChipBackgroundColorResource(R.color.colorAccent); 
chip.setCloseIconVisible(true);
chip.setTextColor(getResources().getColor(R.color.white));
chip.setTextAppearance(R.style.ChipTextAppearance);
chip.setId(ViewCompat.generateViewId());
// edit:
chip.setCheckable(true);
chip.setOnCheckedChangeListener() // could use this per item
chipGroup.addView(chip);
}
}

请记住,您需要设置和id (chip.setId),以便您可以添加一些功能,如获取哪些被选中

编辑:芯片组可以使用OnCheckedChangeListener为芯片组中的所有芯片设置一个回调。这提供了所选芯片的ID。

ChipGroup chipGroup = (ChipGroup)findViewById(R.id.chip_group);
chipGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(ChipGroup group, @IdRes int checkedId) {
// Handle the chip group action.
}
});

最新更新