“This is the 23rd day of my participation in the First Challenge 2022. For details: First Challenge 2022.”
👉 About the author
As we all know, life is a long process of constantly overcoming difficulties and reflecting on progress. In this process, there will be a lot of questions and thoughts about life, so I decided to share my thoughts, experiences and stories to find resonance!!
Focus on Android/Unity and various game development tips, as well as various resource sharing (websites, tools, materials, source code, games, etc.)
Welcome to pay attention to the public account [Mr. Empty name] for more resources and communication!
👉 premise
This is small empty insist to write Android novice series, welcome to taste.
Big guy (×)
Novice (√)
👉 Practice
😜 Chip to monitor
Selected listening: setOnCheckedChangeListener, the monitor only set the checkable property is true or use the filter/entry/choice 】 【 to come into force when the three style theme.
Click event listener: setOnClickListener
The close button was clicked listening: setOnCloseIconClickListener
Java
myChip.setOnCloseIconClickListener(View.OnClickListener { v: View? ->
// Perform service processing
Log.e("TAG"."OnCreate: Icon click event after the Chip text")
})
myChip.setOnClickListener(View.OnClickListener { v: View? ->
// Perform service processing
Log.e("TAG"."OnCreate: Chip whole normal click event") }) myChip.setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener { buttonView: CompoundButton? , isChecked: Boolean ->// Perform service processing
Log.e("TAG"."OnCreate: Chip check status listener")})Copy the code
Kotlin
myChip.setOnCloseIconClickListener(v -> {
// Perform service processing
Log.e("TAG"."OnCreate: Icon click event after the Chip text");
});
myChip.setOnClickListener(v -> {
// Perform service processing
Log.e("TAG"."OnCreate: Chip whole normal click event");
});
myChip.setOnCheckedChangeListener((buttonView, isChecked) -> {
// Perform service processing
Log.e("TAG"."OnCreate: Chip check status listener");
});
Copy the code
😜 ChipGroup listening
myChipGroup.setOnCheckedChangeListener(new ChipGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(ChipGroup group, int checkedId) {
// If the view id is in the layout, it is ok, but in fact, the sub-view should be added according to the interface data, how to distinguish these clicks?
// We will do a complete example in the next article.
Log.e("TAG"."OnCheckedChanged: Select the VIEW ID"); }});Copy the code
😜 instance
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.chip.ChipGroup
android:id="@+id/myChipGroupExample"
android:layout_width="wrap_content"
app:singleSelection="true"
android:layout_height="wrap_content">
</LinearLayout>
Copy the code
Java
public class TestChipAndChipGroupActivity extends AppCompatActivity {
private List<Chip> chipViewList = new ArrayList<>();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chip_chipgroup);
ChipGroup myChipGroupExample = findViewById(R.id.myChipGroupExample);
List<String> dataList = new ArrayList<>();
dataList.add("Sesame seeds");
dataList.add("Here's your business data.");
dataList.add("According to business data");
dataList.add("Create a child View");
dataList.add("Public Account");
dataList.add("Mr. Empty Name");
for (String temp : dataList) {
myChipGroupExample.addView(createChipView(temp));
}
myChipGroupExample.setOnCheckedChangeListener(new ChipGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(ChipGroup group, int checkedId) {
// If the view id is in the layout, it is ok, but in fact, the sub-view should be added according to the interface data, how to distinguish these clicks?
// We will do a complete example in the next article.
for (Chip tempChip : chipViewList) {
if (tempChip.getId() == checkedId) {
Log.e("TAG"."OnCheckedChanged: Select the view ID content:"+ tempChip.getText()); }}}}); }private View createChipView(String temp) {
Chip chipView = new Chip(this);
chipView.setId(View.generateViewId());
chipView.setText(temp);
chipView.setChipIcon(getDrawable(R.drawable.icon_xin));
// Set whether to be selected
chipView.setCheckable(true);
chipView.setTextSize(18);
chipViewList.add(chipView);
returnchipView; }}Copy the code
Kotlin code
override fun onCreate(savedInstanceState: Bundle?). {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_test)
val myChipGroupExample = findViewById<ChipGroup>(R.id.myChipGroupExample)
val dataList: MutableList<String> = java.util.ArrayList()
dataList.add("Sesame seeds")
dataList.add("Here's your business data.")
dataList.add("According to business data")
dataList.add("Create a child View")
dataList.add("Public Account")
dataList.add("Mr. Empty Name")
for (temp indataList) { myChipGroupExample.addView(createChipView(temp!!) ) } myChipGroupExample.setOnCheckedChangeListener { group, checkedId ->// If the view id is in the layout, it is ok, but in fact, the sub-view should be added according to the interface data, how to distinguish these clicks?
// We will do a complete example in the next article.
for (tempChip in chipViewList) {
if (tempChip.id == checkedId) {
Log.e("TAG"."OnCheckedChanged: Select the view ID content:" + tempChip.text)
}
}
}
}
private val chipViewList: MutableList<Chip> = ArrayList()
private fun createChipView(temp: String): View? {
val chipView = Chip(this)
chipView.id = View.generateViewId()
chipView.text = temp
chipView.chipIcon = getDrawable(R.drawable.icon_xin)
// Set whether to be selected
chipView.isCheckable = true
chipView.textSize = 18f
chipViewList.add(chipView)
return chipView
}
Copy the code
👉 other
📢 author: Kom and Kom in Kom
📢 reprint instructions – be sure to specify the source: Zhim Granular’s personal home page – column – Nuggets (juejin. Cn)
📢 the road friend please stay ☁️, I see you extraordinary, talk between the faint king imperious 💚, in the future will have a great as 📝!! Next to a little like 👍 collect 🌟 pass you today, point it, in the future you successful ☀️, I do not take a cent, if not successful ⚡ 🌟, or come back to me.