本文主要是介绍Android-多个tv_item_[i] 点击事件简写,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
private TextView[] tvConstellations = new TextView[12];//获取当前id元素并在其点击的时候修改其颜色
for (int i = 0; i < 12; i++) {int resId = getResources().getIdentifier("tv_constellation_" + (i+1), "id", getPackageName());tvConstellations[i] = findViewById(resId);final int j = i;tvConstellations[i].setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {int color = tvConstellations[j].getCurrentTextColor(); // 获取 TextView 的实际颜色值int targetColor = ContextCompat.getColor(getApplicationContext(), R.color.color_ff7804); // 获取目标颜色的实际值if (color == targetColor) {tvConstellations[j].setTextColor(getResources().getColor(R.color.color_66625e));tvConstellations[j].setBackgroundResource(R.drawable.service_item_background);constellationArr.removeAll(Collections.singleton((j+1)+""));}else{tvConstellations[j].setTextColor(getResources().getColor(R.color.color_ff7804));tvConstellations[j].setBackgroundResource(R.drawable.service_item_selected);constellationArr.add((j+1)+"");}}});}
//获取当前按钮颜色,点击的时候修改其颜色
case R.id.tv_sex_male:{ //男int colorMale = tv_sex_male.getCurrentTextColor(); // 获取 TextView 的实际颜色值int targetColor = ContextCompat.getColor(getApplicationContext(), R.color.color_ff7804); // 获取目标颜色的实际值if (colorMale == targetColor) {tv_sex_male.setTextColor(getResources().getColor(R.color.color_66625e));tv_sex_male.setBackgroundResource(R.drawable.service_item_background);reqServicePerson.setSex("");}else{tv_sex_male.setTextColor(getResources().getColor(R.color.color_ff7804));tv_sex_male.setBackgroundResource(R.drawable.service_item_selected);reqServicePerson.setSex("0");}tv_sex_female.setTextColor(getResources().getColor(R.color.color_66625e));tv_sex_female.setBackgroundResource(R.drawable.service_item_background);break;}
这篇关于Android-多个tv_item_[i] 点击事件简写的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!