【发布时间】:2016-11-18 11:02:20
【问题描述】:
实际上,当我单击按钮时,我有多个矩形按钮,我将获得列表视图。现在,当我单击按钮时,我需要它以红色突出显示,其他所有按钮必须为绿色。请大家帮忙。
活动:
if(name.isEmpty()||name==null){
billnumber +=name;
}
else{
if(count==3){
billnumber +=" , "+name;
}
else if(count==2){
billnumber +=" , "+name;
}
else if(count==1){
billnumber +=name;
}
}
if (count == 3) {
id++;
final Button dynamicTextView = new Button(this);
dynamicTextView.setLayoutParams(new LayoutParams(350,
LayoutParams.MATCH_PARENT));
dynamicTextView.setBackgroundColor(Color.parseColor("#1c7900"));
dynamicTextView.setText("Bill Numbers\n"+billnumber);
dynamicTextView.setId(id);
final Button dynamicinvnumber=new Button(this);
if(id==(clickedid-1)){
dynamicTextView.setBackgroundColor(Color
.parseColor("#cf0000"));
}
if(istouched){
if (id == clickedid) {
touchedlist=dynamicTextView.getText().toString();
if(touchedlist.contains("Bill Numbers"))
{
touchedlist=touchedlist.replace("Bill Numbers","");
}
text=touchedlist;
if(bumpedbillnumber!="0")
{
if(touchedlist.contains(bumpedbillnumber))
{
touchedlist=touchedlist.replace(bumpedbillnumber,"");
}
if(text.contains(bumpedbillnumber))
{
text=text.replace(bumpedbillnumber,"");
}
}
dynamicTextView.setBackgroundColor(Color
.parseColor("#cf0000"));
}
}
if(refreshlist!=1)
{
if(id==1)
{
dynamicTextView.setBackgroundColor(Color
.parseColor("#cf0000"));
}
}
if (invoice1 == null) {
if (id ==1) {
dynamicTextView.setBackgroundColor(Color
.parseColor("#cf0000"));
}
}
dynamicTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 30);
dynamicTextView.setOnClickListener(new OnClickListener() {
@SuppressWarnings("deprecation")
@Override
public void onClick(View v)
{
EnabledButton = dynamicTextView.getId();
clickedid=dynamicTextView.getId();
dynamicTextView.setBackgroundColor(Color
.parseColor("#cf0000"));
dynamicTextView.setSelected(true);
invoiceToDisplay = null;
invoiceToDisplay = new ArrayList<String>();
text = dynamicTextView.getText().toString();
if(text.contains("Bill Numbers"))
{
text=text.replace("Bill Numbers","");
}
String s[] = text.split(" , ");
invoice = text.split(" , ");
System.out.println("s" + s[0]);
istouched=true;
refreshlist=1;
if (s.length == 1) {
if(s[0].contains("\n"))
{
s[0]=s[0].replace("\n","");
}
int invoice11=receiptlist.indexOf(s[0].trim());
String invoiceselected=invoiceList.get(invoice11);
tv1.setVisibility(View.GONE);
tv2.setVisibility(View.GONE);
tv.setVisibility(View.VISIBLE);
footerText3.setVisibility(View.GONE);
footerText2.setVisibility(View.GONE);
loadListViews(invoiceselected, listView1, headerButton
可绘制的 Xml:
<corners android:radius="3dp" />
<solid android:color="#124a01" />
<stroke
android:width="2px"
android:color="#c8ea32" />
【问题讨论】:
-
检查这个stackoverflow.com/questions/3882064/…按钮颜色在点击时的变化
-
只需将您点击的位置传递给适配器并在代码下方设置 condilike if (pos == position) { mListview.setSelection(pos); mCardView.setCardBackgroundColor(getResources().getColor(android.R.color.white)); mAdapter.notifyDataSetChanged(); } else { mCardView.setCardBackgroundColor(getResources().getColor(R.color.blue_trans)); }
-
@sagar 问题是不同的,我希望点击的按钮为红色,其他为绿色
-
@vadivel 我想改变点击按钮的颜色,不在列表视图中按钮不会获得位置
-
声明按钮数组,然后为每个按钮分配id,然后获取点击位置id并使用for循环检查条件