我正在使用3个按钮制作应用。
我有以下带有矩形按钮的.xml文件。
现在我想制作它们,如果选择了一个,那么该按钮变为红色而另外两个保持绿色。
在前进动作中它正常工作:如果我单击c按钮然后返回并单击A按钮,两者都显示相同的红色,如果我单击B并在A按钮之后,然后在出现一些延迟后也显示相同的颜色。
我该如何解决这个问题?
XML:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners android:radius="3dp" />
<solid android:color="#124a01" />
<stroke
android:width="2px"
android:color="#c8ea32" />
</shape>
的活动:
public void onClick(View v) {
DeselectButtons();
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, headerButton1);
invoice1 = invoiceselected;
invoice2 = "";
invoice3 = "";
headerButton2.setText("");
headerButton3.setText("");
adapter = new CustomAdapter(
PendingOrdersActitvity.this, itemsList2);
listView2.setAdapter(adapter);
adapter = new CustomAdapter(
PendingOrdersActitvity.this, itemsList2);
//adapter = new CustomAdapter(getBaseContext(),itemsList2,PendingOrdersActitvity.this);
listView3.setAdapter(adapter);
invnumber = "";
} else if (s.length == 2) {
if(s[0].contains("\n"))
{
s[0]=s[0].replace("\n","");
}
int invoice11=receiptlist.indexOf(s[0].trim());
String invoiceselected=invoiceList.get(invoice11);
int invoice12=receiptlist.indexOf(s[1].trim());
String invoiceselected1=invoiceList.get(invoice12);
invoice1 = invoiceselected;
invoice2 = invoiceselected1;
invoice3 = "";
loadListViews(invoiceselected, listView1, headerButton1);
loadListViews(invoiceselected1, listView2, headerButton2);
adapter = new CustomAdapter(
PendingOrdersActitvity.this, itemsList2);
listView3.setAdapter(adapter);
headerButton3.setText(""); footerText3.setVisibility(View.GONE); tv2.setVisibility(View.GONE);
invnumber = "";
} else if (s.length == 3) {
if(s[0].contains("\n"))
{
s[0]=s[0].replace("\n","");
}
int invoice11=receiptlist.indexOf(s[0].trim());
String invoiceselected=invoiceList.get(invoice11);
int invoice12=receiptlist.indexOf(s[1].trim());
String invoiceselected1=invoiceList.get(invoice12);
int invoice13=receiptlist.indexOf(s[2].trim());
String invoiceselected2=invoiceList.get(invoice13);
tv.setVisibility(View.VISIBLE);
tv1.setVisibility(View.VISIBLE);
tv2.setVisibility(View.VISIBLE);
invoice1 = invoiceselected;
invoice2 = invoiceselected1;
invoice3 = invoiceselected2;
loadListViews(invoiceselected, listView1, headerButton1);
loadListViews(invoiceselected1, listView2, headerButton2);
loadListViews(invoiceselected2, listView3, headerButton3);
invnumber = "";
} //thread.start();
}
});
Button button = new Button(this);
button.setLayoutParams(new LayoutParams(10,
LayoutParams.MATCH_PARENT));
linear.addView(dynamicTextView);
linear.addView(button);
count = 0;
invoiceNumber = "";
// invoice=null;
billnumber = "";
}
}
}
方法:
public void DeselectButtons() {
for (int i = 1; i < id; i++) {
if (EnabledButton != i) {
if (this.findViewById(i) == null) {
} else {
this.findViewById(i).setBackgroundColor(
Color.parseColor("#1c7900"));
}
} else {
this.findViewById(i).setBackgroundColor(
Color.parseColor("#1c7900"));
}
}
}
答案 0 :(得分:2)
取三个RadioButtons
并将它们放在RadioGroup
内,然后将选择器设置为背景,使它们看起来像一个按钮。因此,一次只会选择一个并突出显示。
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="@drawable/btn_radio_off" />
<item android:state_checked="true" android:drawable="@drawable/btn_radio_on" />
</selector>
这样您就不需要手动处理更改和颜色了。
答案 1 :(得分:0)
最初将所有按钮保持为绿色。跟踪上次点击的按钮。
int previousbutton = 0;
然后在onclick上:
public void onClick(View v){
if(previousbutton != 0){
findViewById(previousbutton).setBackgroundColor(//set green color);
}
v.setBackgroundColor(//set red color);
previousbutton = v.getId();
}